Using Javascript in aspx

legendgod

Regular
Joined
May 17, 2004
Messages
53
Location
Hong Kong
Because I want to apply some Javascript function I found on web, I use RegisterStartupScript. The following is place at the first sentence of Page_Load:
Code:
  Page.RegisterStartupScript("PopUpTextScript","<script language=javascript src='PopUpText.js'>")
However, when the aspx is run, the <script language=....></script> tab is stand at the last position of <form></form> tab. It makes the Javascript do not run correctly.

How to correct it? Thank you.
 
Don't place it in your page_load, overload the Render(HtmlTextWriter writer) method and place it before base.Render(writer); is called.
 
Have you tried putting it in Page_PreRender event instead of Page_Load?

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
'Your code here
End Sub
 
Thanks

bob01900 said:
Have you tried putting it in Page_PreRender event instead of Page_Load?

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
'Your code here
End Sub

Thank you. When I add following, the javascript runs well.... but because lack of close tab: "</script>" All the html code after this tab become invalid and lost.
However, when I type the code like that:
Page.RegisterStartupScript("alertScript","<script language=javascript src=AlertScript.js></script>")It makes error... is it any escape character for the </script> ?
 
Last edited:
wessamzeidan said:
I think it works without </script>. Try it
Well... I tried and success. However, when the code come to client side, I left a open tab: <script language=.....> towards the code. Therefore all the HTML code after the open tab lost their function. They cannot be seen in Internet explorer.
 
bob01900 said:
Have you tried putting it in Page_PreRender event instead of Page_Load?

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
'Your code here
End Sub
Can I ask how should I rewrite the part "MyBase"? What is it define as?
 
Page.RegisterStartupScript("alertScript",@"<script language=javascript src=AlertScript.js></script>")

Do you see the @ ?
it will permit you to go throught this one.

Tell me if it work
 
As a supp. note...

This would probably not work with old Netscape Browser. I know there's few that use it... but it's only to tell you.

My company use Netscape 4.75 as a default browser for the WHOLE company. And it's not a small company... there's about 30,000 employee. So no need to tell you that, us... programmer... we can't get things to work properly 100% of the time.
 
legendgod said:
Can I ask how should I rewrite the part "MyBase"? What is it define as?

MyBase refers to the base class that your class/form inhereted from. ie. In your code, there should be something similar to the follwoing (somewhere at the top):

Code:
Public Class WebForm1
    Inherits System.Web.UI.Page
...

And PreRender is an event inhereted from the Page class. I am not sure what you meant by "rewrite", you mean override functions or??

As for "</script>", I think you need it, or use what wessamzeidan suggested, close it with "/>". But I have no idea why "</script>" would fail. What kind of error are you getting?
 
Back
Top