JavaScript file loading problem

tfowler

Regular
Joined
Aug 16, 2005
Messages
84
Location
Columbus, OH
I am having trouble loading javascript from a file into my ASP.NET 2.0 page. If I copy the contents of the js file into my aspx page, it works. However, if I try loading it from the file called "NumericTextBoxKeyPress.js", it gives me a "A Runtime Error has occured. Do you wish to Debug? Line: 1 Error: Syntax error". Selecting yes takes me to the "NumericTextBoxKeyPress.js" file in Visual Studio, but does not highlight anything. What could be the problem?

Contents of the "NumericTextBoxKeyPress.js" file:
Code:
[size=2][color=#0000ff]<[/color][/size][size=2][color=#800000]script [/color][/size][size=2][color=#ff0000]type [/color][/size][size=2][color=#0000ff]= 'text/javascript'>[/color][/size][indent][size=2][color=#0000ff]function[/color][/size][size=2] NumericTextBoxKeyPress(sender, [/size][size=2][color=#0000ff]event[/color][/size][size=2])

 
[/size][size=2]{[/size]
 
 


[size=2][indent][/size][size=2][color=#0000ff]var[/color][/size][size=2] curChar = String.fromCharCode([/size][size=2][color=#0000ff]event[/color][/size][size=2].keyCode);

 
[/size][size=2][color=#0000ff]var[/color][/size][size=2] inpStr = sender.value + curChar;
result = inpStr.match([/size][size=2][color=#800000]'^[0-9.]+$'[/color][/size][size=2]);
[/size][size=2][color=#0000ff]if[/color][/size][size=2] (!result)
 
 


{[/size][indent][size=2][color=#0000ff]event[/color][/size][size=2].returnValue = [/size][size=2][color=#0000ff]false[/color][/size][size=2];

 
[/size][size=2][color=#0000ff]event[/color][/size][size=2].cancel = [/size][size=2][color=#0000ff]true[/color][/size][size=2];[/size]
 

[/indent][size=2]}[/size]

 
 

[/indent][size=2]}[/size][size=2]

 
 

[/indent][/size]</script>

I try to load it in my Page.Load event like so:

Visual Basic:
[size=2][size=2][color=#0000ff]If [/color][/size][size=2][color=#0000ff]Not [/color][/size][size=2]IsPostBack [/size][size=2][color=#0000ff]Then[/color][/size][indent][/size][size=2]ClientScript.RegisterClientScriptInclude([/size][size=2][color=#800000]"NumericTextBoxKeyPress"[/color][/size][size=2], [/size][size=2][color=#800000]"NumericTextBoxKeyPress.js"[/color][/size][size=2])[/size]

 
 

[/indent]End If

Thanks for any help you can provide,

Todd
 
Last edited:
Hi,

I have the same problem as you too.

To solve it, I put the jscript in the aspx.vb file under the Page_Load method.
RegisterStartUpScript is used.

e.g Page.RegisterStartUpScript("MyScript",_
"<script language='javascript'>" & _
"function Hello() {alert('Hello');} </script>")

If u wan the jscript to run when the page loads, use window.onload

e.g window.onload = function Hello() {......
 
Thanks for the suggestions.

fizzled: Your suggestion worked like a charm. I guess since it is a "*.js" file, it already knows what kind of script it is.

LiLo: I was trying to keep the actual script outside of the individual pages, since I want to be able to reuse the script on mutiple pages. Thanks for the suggestion though.

Todd
 
Back
Top