HEEN
HEEN

Reputation: 4727

Call window.onload function in code behind not working

I have written a js function in aspx like below

    window.onload = function () {
        document.getElementById('grid-overlay').style.display = 'block';
    }

Now I want to call this function in code behind. I tried like below:-

 if (dtmkey.Rows.Count > 0)
   {
       HidMode.Value = "M";
       HidMKey.Value = dtmkey.Rows[0]["mkey"].ToString();

       var scriptSource = "function onload() { alert(""); };\n";
       ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "HelpScript", scriptSource, true);  // Not working
   }

kindly suggest what is wrong

Upvotes: 0

Views: 5688

Answers (1)

brk
brk

Reputation: 50326

window.onload is an event handler for the load event of a window & the attached function is function that will be called on load event.

Also it is not clear what you are trying to do at this point

// I ran this variable in console, it threw unexpected syntax
var scriptSource = "function onload() { alert(""); };\n"; //

Are you trying to call a function expression here? you can make this changes and test it

function onload(){  // This is a function expression and onload not same as window.onload
        document.getElementById('grid-overlay').style.display = 'block';
}

window.onload = function () {
        onload() // call onload function here once window has finished loading
    }

If you are looking for onload here you can replace the below snippet

var scriptSource = "function onload() { alert(""); };\n"; with `onload();`

Upvotes: 2

Related Questions