making sure page loads

G

Guest

hey all,
i have a button on my web form that after it posts back it suppose to throw
up an alert. it works fine but that it throws the alert before the entire
page has been displayed. is there any way i can get it to display the alert
after the entire page is displayed?

thanks,
rodchar
 
R

Rob

rodchar,

To do this you need to display the alert from the BODY tag's onload method.

OnLoad can be a bit of a pain to handle correctly, especially if you have 2
or more bits of code that need OnLoad handlers, not to mention cross browser
support.

Here's a chunk of code I like for onload (handles multiple onload handlers
and is cross browser compatabile):

//
// Add the addLoadEvent function
// - A clean cross browser method to add a window.onload method
//
ClientScript.RegisterClientScriptBlock(this.GetType(), "addLoadEvent",
"function addLoadEvent(func) { var oldonload = window.onload; if (typeof
window.onload != 'function') { window.onload = func; } else { window.onload
= function() { if (oldonload) { oldonload(); } func(); } } }", true);

More or less the above checks if there is an existing OnLoad handler. If
there isn't then the function passed as the argument is used. If there is an
existing onload handler then a new function is defined that calls the
existing onload handler and then calls the function passed as the argument.
This new function is then setup as the onload handler.

With that javascript function defined,you can then add as many onload
handlers as you need. To display an alert after the page has fully loaded:

string ShowMyAlert = "addLoadEvent(function() { alert('hello'); });";
ClientScript.RegisterStartupScript(this.GetType(), "ShowMyAlert",
ShowMyAlert, true);

Regards,

Rob
 
G

Guest

thank you for the help.

Rob said:
rodchar,

To do this you need to display the alert from the BODY tag's onload method.

OnLoad can be a bit of a pain to handle correctly, especially if you have 2
or more bits of code that need OnLoad handlers, not to mention cross browser
support.

Here's a chunk of code I like for onload (handles multiple onload handlers
and is cross browser compatabile):

//
// Add the addLoadEvent function
// - A clean cross browser method to add a window.onload method
//
ClientScript.RegisterClientScriptBlock(this.GetType(), "addLoadEvent",
"function addLoadEvent(func) { var oldonload = window.onload; if (typeof
window.onload != 'function') { window.onload = func; } else { window.onload
= function() { if (oldonload) { oldonload(); } func(); } } }", true);

More or less the above checks if there is an existing OnLoad handler. If
there isn't then the function passed as the argument is used. If there is an
existing onload handler then a new function is defined that calls the
existing onload handler and then calls the function passed as the argument.
This new function is then setup as the onload handler.

With that javascript function defined,you can then add as many onload
handlers as you need. To display an alert after the page has fully loaded:

string ShowMyAlert = "addLoadEvent(function() { alert('hello'); });";
ClientScript.RegisterStartupScript(this.GetType(), "ShowMyAlert",
ShowMyAlert, true);

Regards,

Rob
 

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