Brief Ajax object creation code

Source: Internet
Author: User

If your script is only developed for a browser, it is very easy to create XMLHTTP. Use XMLHttpRequest or ActiveXObject. But in fact, most of the time, we have to consider compatibility, So we usually write:

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

Of course, skilled friends prefer concise code:

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

But that's all. Can this code be further compressed? Let's explore it.

Now we rearrange a bunch of words: ActiveXObject, "Microsoft. XMLHTTP", XMLHttpRequest, and window with several symbols to combine a regular expression with correct syntax.

The first thing we can think of most easily is sharing a new one. Because JS has a very powerful operator "|", I believe everyone has used it. So we first use | to get the Class, and then use the new instance to create the instance. So:

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

Unfortunately, it fails to pass the test of IE6 and 7. (IE8 + already supports XMLHttpRequest) the error is very simple, just on ActiveXObject ("Microsoft. XMLHTTP. The Automation server cannot create objects.

In IE, new must be included before ActiveXObject (...); otherwise, the above error will occur. Note that ActiveXObject is called with parameters. What if we reflect the ActiveXObject function separately and then instantiate it? Let's test:

var ref = ActiveXObject;var x = new ref("Microsoft.XMLHTTP");x.open("GET", "1.html", true);x.send();alert(x.responseText)

No error is reported, and the text is displayed successfully. This indicates that the window. ActiveXObject method can be called by reflection, but the new method must be used to create the component. This is easy to understand: the Native Code of function ActiveXObject () {} will judge whether it is new call or direct call. If it is not new call, an error is returned. This means that you cannot create an XMLHTTP Class in advance, but must use the factory form of ActiveXObject.

In this case, we can only use logic such as XMLHttpRequest | ActiveXObject. With new, we will get the following code:

new (window.XMLHttpRequest || ActiveXObject)

This is enough for creating XHR, but ActiveX also needs to provide a parameter to specify the component name. Where can this parameter be added? You don't have to worry about it.

Because the XMLHttpRequest constructor does not contain parameters, and JS is a weak type language, you do not need to specify a parameter for it. Finally, we can use a line of expression to create:

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

Because IE8 + supports XMLHttpRequest and ActiveXObject, the preceding | order determines which IE8 + is preferred.

However, this is not the simplest! Yes, there are more streamlined ones, but they are not very formal or even a bit obscure.

Throughout the above Code, the window seems redundant, but cannot be removed. If there is a simple way to judge whether it is (or not) IE browser, we can use? : Operator to replace |.

A foreigner once wrote a brief method to judge IE. It only takes 6 bytes! -[1,] the principle is the unique feature of IE, And the array handles the final bug. Using this method, we can further compress the Code:

new(-[1,]?XMLHttpRequest:ActiveXObject)("Microsoft.XMLHTTP")

Undoubtedly, this is the simplest XMLHTTP creation code! Exactly 60 bytes. However, compared with normative and readability, It is recommended before.

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

new(self.XMLHttpRequest||ActiveXObject)("Microsoft.XMLHTTP")

It is exactly 60 bytes and is more standard :)

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.