我在aspx中编写了一个js函数,如下所示
window.onload = function () {
document.getElementById('grid-overlay').style.display = 'block';
}
现在我想在代码后面调用这个函数。我尝试如下:-
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
}
请指出哪里出了问题
最佳答案
window.onload
是 window
的 load
事件的事件处理程序,附加的 function
是将在 load
事件中调用的function
。
目前还不清楚你想做什么
// I ran this variable in console, it threw unexpected syntax
var scriptSource = "function onload() { alert(""); };\n"; //
你想在这里调用函数表达式吗? 您可以进行此更改并进行测试
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
}
如果您正在此处查找onload
,您可以替换以下代码段
var scriptSource = "function onload() { alert(""); };\n"; with `onload();`
关于javascript - 在代码后面调用 window.onload 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35883093/