How do these lines of JavaScript code make your browser crash?

Source: Internet
Author: User

  first on Demo, interested can try first:

http://www.zeakhold.com/crash/

  (Warm tip: Please save the task of other windows of browser before visit, the trick is not responsible for playing the joke ~)

   things still have to go from A tweet from IT Security tweets™ says:

That is to say, this JavaScript code, can let the browser crash and can let the iphone restart?!

  so open the computer eager, when full of curiosity to execute the code on the browser after the discovery: Chrome flew into the state of the card, the more deadly is to click the Close window did not respond!!

Open Task Manager and discover that the CPU has soared to 100%:

Stop Chrome right away and it's okay to get back to normal.

  But also wonder, what is the stem of these lines of code?

<script>    var total = "";      for (var i = 0; i < 1000000; i++) {        = total + i.tostring ();        History.pushstate (0, 0, total);    } </script>

  The Pushstate () method, which looks at the history object from the code , exhausts the system's resources in 1 million cycles. Looked at the relevant blog introduction, only to find that the original pushstate () is one of the new features introduced by HTML5, its introduction and Ajax has a close relationship.

We know that the advent of Ajax facilitates users to browse the Web page, it allows users to update the content of the site without refreshing, but this also raises the issue, that is, after updating the content of the site, there is a difference between different pages, And this difference can not be reflected in the URL: the page changes generated by Ajax are not accompanied by changes in the URL, the current page URL is still the same as the previous page URL, which caused us to go forward, backward to switch pages. In the traditional browsing experience, changes in the content of the page are often accompanied by changes in the URL, which correspond to "forward" and "backward", but the advent of Ajax undermines this unique experience. To do this, HTML5 has added some features to the history object to solve the problem, including the Pushstate () method in the code above.

According to the HTML5 document, the Pushstate () method is used to add records to the browser's history stack (pushes the given data onto the session), which contains three parameters: an Event object, The title of a page that is added to the history (usually ignored by the browser), an address that is added to the history record. In this way, when AJA make changes once, you can use the Pushstate () method to add a history, based on the addition of several other methods, so that we can proactively edit the history, no refresh to change the URL, so as to compensate for the use of Ajax caused by this flaw.

   back to the code, for the 1 million cycles, the browser's history (push-in URL) has been modified 1 million times, and each loop of the URL is in the previous base overlay, This constantly loops down to history to add records and quickly consumes system memory resources, causing the browser to crash.

After understanding the reason, selected a few mobile devices to do the test, although there is no Twitter said to let the iphone device restart phenomenon, but all coincide with the browser:

IPhone 6s (Safari)--  flash the Apple icon directly, back to the main screen after 3 seconds, return to normal (this does not count restart it);
4(Safari)--  stuck in browser interface, forced to close the browser after the return to normal;
Huawei P6 (built-in browser)--card dead in the browser interface, forced to close the browser to return to normal;
Meizu MX3 (built-in browser, UC Browser)--card dead in the browser interface, forced to close the browser after the return to normal;
QQ, also tried to open the page, the results are flash back to the main interface chat.

  with This code, there seems to be a lot of tricky little friends new posture Hehe ~

(reproduced please specify the source--http://www.cnblogs.com/zeakhold/)

How do these lines of JavaScript code make your browser crash?

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.