In the service side (Page.write) invoke the custom JS method to explain the _javascript tips

Source: Internet
Author: User
Tags setinterval
First of all, we should be clear first, why we use Page.write to the custom JS method output to the page why IE is not recognized, there will be "xxx undefined" error. The reason is simple, because the script we output with Page.write is at the top of the page. IE read the JavaScript function, it began to execute, but at this time we link JS file is not read by IE, so IE can not identify our definition in the JS file inside the method. Why would write alert do that? Because alert is an IE-embedded scripting function, IE recognizes it regardless of the page.
Find out where the problem is, and it's all settled:
1. Embed our custom approach into IE. ---> looks a little whimsical, huh?
2, wait for the page load completed and then trigger the event. ---> triggers the event, yes.
How do I know if the page is loaded and completed?
1, through the state of document
2, through the event trigger (Windows.onload)
The first one seems to be more uninsured, sometimes it is all finished, it is still displayed in the transmission of data (FF this situation most obvious), so still with the incident more insurance.
Define a simple method, mount it inside the windows.onload, and do a logo when executed
Copy Code code as follows:

var loadcomplete = false;
function loadcompleted ()
{
Loadcomplete= true;
}
Window.attachevent ("onload", loadcompleted);

Oh, so we just judge LoadComplete to get the page is loaded.
Copy Code code as follows:

var mimgdir = "";
var mcaption = "caption";
var mmsg = "message";
var mokclick= null;
function ShowMessage (Imgdir,caption,msg,okclick)
{
if (LoadComplete)
{
Kmessagebox.showinfo (Mimgdir,mcaption,mmsg,mokclick);
}
}

Such a child in the case of LoadComplete not false, we will not go to execute the Kmessagebox.showinfo () method, only then will not appear JS error prompts.
Just like this, not yet, because the output of such a script, ie only in the output of the page when the execution, but at this time loadcomplete=false, so we need to periodically check whether the page is loaded. When it comes to timing, it's only a settimeout & SetInterval. We need to keep testing here, so we use the SetInterval method. The final code is as follows:
Copy Code code as follows:

var loadcomplete = false;
var mimgdir = "";
var mcaption = "caption";
var mmsg = "message";
var mokclick= null;
var Timerid;
function ShowMessage (Imgdir,caption,msg,okclick)
{
if (LoadComplete)
{
Kmessagebox.showinfo (Mimgdir,mcaption,mmsg,mokclick);
Uninstall this event window.detachevent ("onload", function () {loadcompleted;}); Stop timed Trigger
Window.clearinterval (Timerid);
}
}
function loadcompleted () {loadcomplete=true;}
Window.attachevent ("onload", loadcompleted);
Set up timing detection mechanism
Timerid = Window.setinterval (showmessage,1);

Of course, the above code only compatible with IE, because the use of attachevent and detachevent, as well as other browsers to allow him to see [JavaScript] custom title display way in the article How to:
Copy Code code as follows:

if (!document.attachevent)//not IE
{
Document.attachevent = function () {Document.addeventlistener (Arguments[0].substr (2), arguments[1],arguments[2])}
}
if (!window.attachevent)//not IE
{
Window.attachevent = function () {Window.addeventlistener (Arguments[0].substr (2), arguments[1],arguments[2])}
}

On the server side, just StringBuilder out the script and write it out. Above also just provides a kind of thinking, of course there will be other methods, such as I do not apply to the timing test, I directly mounted to the windows.onload inside, so that the page automatic monitoring, automatic implementation, also may be:), is called All Roads pass Romama ~~~~~
The above ideas from Yui, and Yui realized a more beautiful custom MessageBox, interested friends can study together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.