[Challenge limit] the shortest Ajax creation code

Source: Internet
Author: User

In the afternoon, I wrote an article about loading scripts in the shortest address bar. However, it's just entertaining. I just remembered that it would be better to shorten the code that is often used at ordinary times, so now I am writing and researching, and creating the shortest XMLHTTP build.

 

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. It is 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 reference 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 referenced and called, but it must be created using new. 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? Don't worry too much, just put it at the end!

  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.

  

  

Supplement: I would like to thank crimsonet users on the classic forum for reminding me that the self object can replace 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 :)

(If it is not run in the embedded framework, Top = Window and-1 can also be used. Top references to top-level forms in the framework, of course there will be problems)

  
 

 

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.