Javascript alerts - work on some pages but not on others

T

thatsMaBoy

Hi,

I am attempting to display a simple Javascript alert when a webpage
loads. It works on some pages but not on others. For example, the
following code does not produce an alert:

Code in webpage1 (note, this is the only VB code in the ASPNET
webpage, in other words, the rest is all HTML):

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not IsPostBack Then
Dim str As String = "<script
language=javascript>alert('Hello');</script>"
Page.RegisterStartupScript("Startup", str)
End If
End Sub

However, the same code on a different page produces an alert:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not EventLog.SourceExists("Building Portal") Then
EventLog.CreateEventSource("Building Portal",
"Application")
End If
EventLog1.Source = "Building Portal"
EventLog1.Log = "Application"

If Page.IsPostBack Then
'make checks to see if the user or email address is
already registered or in process of being registered
Call checkDetails()

Select Case True
Case bEmailExists
lblMessage.Text = "Email already exists! Please
select another"
bDoNotReg = True
Case bUserExists
lblMessage.Text = "Username already exists! Please
select another"
bDoNotReg = True
End Select
End If
End Sub

this page has a lot more VB code...

Please could someone explain why the alert should work on one page but
not the other? Is there some setting that needs setting (I don't
think there is since I haven't actually set one).

Thanks.

Jimmy
 
T

thatsMaBoy

Hi,

I am attempting to display a simple Javascript alert when a webpage
loads. It works on some pages but not on others. For example, the
following code does not produce an alert:

Code in webpage1 (note, this is the only VB code in the ASPNET
webpage, in other words, the rest is all HTML):

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not IsPostBack Then
Dim str As String = "<script
language=javascript>alert('Hello');</script>"
Page.RegisterStartupScript("Startup", str)
End If
End Sub

However, the same code on a different page produces an alert:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not EventLog.SourceExists("Building Portal") Then
EventLog.CreateEventSource("Building Portal",
"Application")
End If
EventLog1.Source = "Building Portal"
EventLog1.Log = "Application"

If Page.IsPostBack Then
'make checks to see if the user or email address is
already registered or in process of being registered
Call checkDetails()

Select Case True
Case bEmailExists
lblMessage.Text = "Email already exists! Please
select another"
bDoNotReg = True
Case bUserExists
lblMessage.Text = "Username already exists! Please
select another"
bDoNotReg = True
End Select
End If
End Sub

this page has a lot more VB code...

Please could someone explain why the alert should work on one page but
not the other? Is there some setting that needs setting (I don't
think there is since I haven't actually set one).

Thanks.

Jimmy

Apologies for my first post, the second webpage (in which the alert
works) should have contained the following code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not EventLog.SourceExists("Building Portal") Then
EventLog.CreateEventSource("Building Portal",
"Application")
End If
EventLog1.Source = "Building Portal"
EventLog1.Log = "Application"

If Page.IsPostBack Then
Dim str As String = "<script
language=javascript>alert('Click on the button?');</script>"
Page.RegisterStartupScript("Startup", str)

'make checks to see if the user or email address is
already registered or in process of being registered
Call checkDetails()

Select Case True
Case bEmailExists
lblMessage.Text = "Email already exists! Please
select another"
bDoNotReg = True
Case bUserExists
lblMessage.Text = "Username already exists! Please
select another"
bDoNotReg = True
End Select
End If
End Sub

etc, etc, (i.e more code in page)

Thanks.
 
B

bruce barker

most likely you have a script error on the page. the first script error
cancels script processing. turn on script debugging in the browser.

-- bruce (sqlwork.com)
 
P

Patrice

Don't know if related but you used "If not IsPostBack" on one and "If
postback" on the other so it shouldn't be triggered at the same time in both
pages...

Also as this is a a client side problem an easy way to diagnose this would
be to use "view source" and check if the rendered HTML contains what you
expect...
 
R

Random

Are you using RegisterStartupScript anywhere else on the page? I could be
wrong about this, but the first parameter to that method is supposed to be a
unique to what you are trying to register. "Startup", then, in your
example, would not properly register if you used that unique string to
register some other startup script. You aren't allowed to overwrite.
 
T

thatsMaBoy

Are you using RegisterStartupScript anywhere else on the page? I could be
wrong about this, but the first parameter to that method is supposed to be a
unique to what you are trying to register. "Startup", then, in your
example, would not properly register if you used that unique string to
register some other startup script. You aren't allowed to overwrite.

Thanks for the replies/suggestions. RegisterStartupScript is only
used once on the page that works, and also only once on the page that
does not work. They are also registered with different names (i.e.
"Startup" and "SomethingElse").

I have checked the generated source and find that the Javascript code
is not present in the page that does not work (hardly surprising).

I have also tried changing from NOT PAGE.ISPOSTBACK to PAGE.ISPOSTBACK
(and vice versa) for the code that registers the Javascript, but still
no joy.

Thanks.

Jimmy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top