得到javascript运行时错误:myfunction未定义 [英] Got javascript runtime error: myfunction is not defined

查看:155
本文介绍了得到javascript运行时错误:myfunction未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码隐藏代码中,

In my code-behind code,

Page.ClientScript.RegisterStartupScript(
     GetType(),
     "Javascript",
     "javascript:myFunction();",
     true);
//// --------- Below is the JavaScript function in .aspx
function myFunction() {
   debugger;
}     



得到未捕获的引用错误(或JavaScript运行时错误):myFunction未定义。怎么调试呢?谢谢。



我尝试过:



得到了JavaScript运行时错误:xxx未定义


Got Uncaught reference error (or JavaScript runtime error): myFunction is undefined. How can this be debugged? Thanks.

What I have tried:

Got JavaScript Runtime error: "xxx" is not defined

推荐答案

JavaScript从上到下执行。这就是为什么你需要首先定义函数然后调用函数

在这种情况下 ClientScript.RegisterStartupScript 在启动时注册脚本(加载文档时), myFunction 在页面底部定义,这意味着你的 myFunction 仍未被浏览器执行..

您可以将 RegisterStartupScript 更改为 RegisterClientScriptBlock 在定义 myFunction 之后......
JavaScript is executed top to bottom approach. that's why you need to first define the function and then call the function.
In you case the ClientScript.RegisterStartupScript Register the Script on the startup(when the document is loaded), your myFunction is defined at bottom of the page,that's means your myFunction still not been executed by the browser..
You can change the RegisterStartupScript to RegisterClientScriptBlock after the the myFunction is defined...


这篇关于得到javascript运行时错误:myfunction未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆