简体   繁体   English

在背后的代码中调用window.onload函数不起作用

[英]Call window.onload function in code behind not working

I have written a js function in aspx like below 我已经在aspx中编写了一个js函数,如下所示

    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 请暗示出什么问题了

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. window.onloadwindow load事件的事件处理程序,附加function是将在load事件时调用的function

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 如果您要在此处查找onload ,则可以替换以下代码段

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM