0

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

1 Answer 1

2

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();` 
Sign up to request clarification or add additional context in comments.

9 Comments

what is with onload(); ?? i didnt get that
if you are looking this part to get executed document.getElementById('grid-overlay').style.display = 'block'; then there you can call onload function
is this is fine ? var scriptSource = "onload();"; ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "HelpScript", scriptSource, true);
i tried ur code, getting error as Out of stack space error at onload()
I think that is not a fine since var scriptSource = "onload();" this will result it unexpected syntax; instead you can just write onload(); it is actually calling onload function that we defined
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.