Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my code-behind code,
C#
Page.ClientScript.RegisterStartupScript(
     GetType(),
     "Javascript",
     "javascript:myFunction();",
     true);
//// --------- Below is the JavaScript function in .aspx
function myFunction() {
   debugger;
}     

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
Posted
Updated 1-Jul-16 19:25pm
v4
Comments
Karthik_Mahalingam 1-Jul-16 9:14am    
tested your code, it works perfect, the problem is placing the code in right place.
you will have to provide more information or relevant code for understanding the issue.
ZurdoDev 1-Jul-16 9:20am    
Is xxx the exact error? If not, give the exact error. If it is, then somewhere you are calling something called xxx and it does not exist.
s yu 1-Jul-16 9:28am    
Here xxx = myFunction
ZurdoDev 1-Jul-16 9:30am    
You need to be very clear. Post EXACT errors. Wherever you are calling it from does not see it. I don't think you need "javascript:myFunction()", you can just do "myFunction();" but I don't know if that would make much difference.
s yu 1-Jul-16 9:33am    
Tested w/ and w/ javascript: before myFunction. The error message is identical.

1 solution

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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900