[JavaScript] how to call a custom js method on the server (page. Write)

Source: Internet
Author: User

How to call custom JS methods on the server (page. Write)

-Written by prodigal son @ cnblogs.com (06-08-28)

Since the publication of the [JavaScript] custom MessageBox article, many netizens have asked how to call the showinfo method on the server end and thought of a compromise during weekend break.

First, we should be able to clarify why the "XXX undefined" error occurs when we use page. Write to output the custom js method to the page why ie cannot be identified. The reason is simple, because the Script output by page. Write appears at the top of the page. When ie reads JavaScript Functions, it starts to execute. However, at this time, the link JS file is not read by IE, So Ie cannot identify the methods we define in the JS file. Why can write alert? Because alert is an embedded script function of IE, ie recognizes it whether there is a page or not.

 

Find the problem and solve it:

1. Embedded our custom method into IE. ---> It looks a little whimsical.

2. Trigger the event after loading the page. ---> Trigger the event. That's right.

 

How do I know whether the page is loaded?

1. The status of the document

2. Event-triggered (Windows. onload)

 

First, it seems that it is not safe. Sometimes it is clearly loaded, and it still shows that it is transmitting data (FF is the most obvious), so it is safer to use events.

 

Define a simple method, mount it to Windows. onload, and make an identifier during execution

 

 
VaRLoadcomplete =False;FunctionLoadcompleted () {loadcomplete =True;}Window. Attachevent ("Onload", Loadcompleted );

In this way, we only need to judge whether the page has been loaded by loadcomplete.

 

VaRMimgdir ="";VaRMcaption ="Caption";VaRMmsg ="Message";VaRMokclick =Null;FunctionShowmessage (imgdir, caption, MSG, okclick ){If(Loadcomplete) {kmessagebox. showinfo (mimgdir, mcaption, mmsg, mokclick );}}

In this way, when loadcomplete is not false, we will not execute the kmessagebox. showinfo () method, but there will be no JS error prompt.

 

This is not the case. Because the output script is executed only once when the page is output, but loadcomplete = false at this time, we need to regularly check whether the page has been loaded. When it comes to timing, we only sacrifice setTimeout & setinterval. We need to constantly detect it here, so we use the setinterval method. FinalCodeAs 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 triggeringWindow.Clearinterval(Timerid );

}

}

FunctionLoadcompleted () {loadcomplete =True;}

Window. Attachevent ("Onload", Loadcompleted );

// Set the timed Detection Mechanism
Timerid =Window.Setinterval(Showmessage, 1 );

 

Of course, the above Code is only compatible with IE, because attachevent and detachevent are used. For compatibility with other browsers, refer to the processing method in [JavaScript] custom title display method:

 

 
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, as long as stringbuilder generates the above script and then writes it out. The above only provides a way of thinking, of course there will be other methods, such as I do not apply to timed detection, I directly mount to Windows. in onload, let the page automatically listen and execute automatically, and it's not a try :). What's the so-called Great road to Rome ~~~~~

 

The above idea comes from Yui, and Yui implements a more beautiful custom MessageBox. If you are interested, you can study it together.

Related Article

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.