Short creation of Ajax object code

Source: Internet
Author: User

If your script is developed for only one browser, then creating a XMLHTTP is a simple thing to do with XMLHttpRequest or ActiveXObject. But in fact most of the time, we have to consider compatibility, so we usually write: Laishui County beam to paper

var x;if (window. ActiveXObject)    x = new ActiveXObject ("Microsoft.XMLHTTP"); else    x = new XMLHttpRequest ();

Of course, skilled friends are more inclined to concise code:

var x = window. ActiveXObject?        New ActiveXObject ("Microsoft.XMLHTTP"):        new XMLHttpRequest ();

But that's the only thing. Can this code continue to compress? We might as well explore the next.

Now we put together a bunch of words: ActiveXObject, "Microsoft.XMLHTTP", XMLHttpRequest, window with a few symbols rearranged, to assemble a syntactically correct and functioning expression.

First of all, the easiest thing to think about is to share a new one. Because JS has a very powerful operator "| |", I believe everyone has used. So let's start with | | The class is obtained, and then the instance is instantiated with new. So:

New (window. XMLHttpRequest | | ActiveXObject ("Microsoft.XMLHTTP"))

Unfortunately, it failed to pass the ie6,7 test. (ie8+ has supported XMLHttpRequest) the error is simple, on ActiveXObject ("Microsoft.XMLHTTP"). The Automation server cannot create an object.

In IE, ActiveXObject (...) You must bring new before, or the error will appear above. Note, however, that the ActiveXObject is followed by a parameter call. If we were to reflect the function of ActiveXObject and then instantiate it, what would the result be? We test under:

var ref = Activexobject;var x = new ref ("Microsoft.XMLHTTP"), X.open ("GET", "1.html", true); X.send (); Alert ( X.responsetext)

Results not only did not error, and successfully displayed the text. This shows the window. ActiveXObject This method is capable of reflection calls, but must use new to create the build. This is well understood: native code of function ActiveXObject () {} will determine whether it is currently new call or direct call, and if not new call, an error. This means that you cannot pre-create the XMLHTTP class, but must use the ActiveXObject factory form.

Since it is true, we can only use XMLHttpRequest | | ActiveXObject such a logic. If you bring new, we get this code:

New (window. XMLHttpRequest | | ActiveXObject)

This is enough to create the XHR, but ActiveX also needs to provide a parameter to specify the component name. Where does this parameter add to the good? Don't worry about it, put it in the end.

Because XMLHttpRequest's constructor is a non-parametric, and JS is a weakly typed language, you won't have a problem if you give him a parameter. So finally, we can create it with one line of expressions:

New (window. XMLHttpRequest | | ActiveXObject) ("Microsoft.XMLHTTP")

Because ie8+ supports XMLHttpRequest and ActiveXObject, the above | | The left and right order determines which ie8+ to use first.

However, this is not the shortest! Yes, there are more streamlined, but not particularly formal, or even a bit obscure.

Looking at the code above, the window looks a bit redundant, but it can't be removed. If there is an easy way to tell if it is (or isn't) ie, we can use the?: operator instead of | | The

Once a foreigner wrote the shortest way to judge ie, only 6 bytes!-[1,] the principle is IE unique features, array processing the last item of the bug. With this approach, we can also compress the code further:

New (-[1,]? Xmlhttprequest:activexobject) ("Microsoft.XMLHTTP")

Undoubtedly, this is the shortest XMLHTTP creation code! Exactly 60 bytes. But compared to the normative and readability, it is recommended before that.

In fact, using the Self object can completely replace window. Self is a read-only property of window and always points to window, so use:

New (self. xmlhttprequest| | ActiveXObject) ("Microsoft.XMLHTTP")

Also happens to be 60 bytes, and more canonical:)

Short creation of Ajax object code

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.