JsIE8 does not support forEach Solutions

Source: Internet
Author: User
We all know that forEach in IE8 and earlier versions does not support js. How can we make IE8 or earlier versions support forEach? This article introduces the solution that IE8 does not support forEach. For more information about the code farmers, see. First, let's determine whether the browser supports js forEach. The Code is as follows:

If (typeof Array. prototype. forEach! = 'Function') {// not supported. In this case, we need to define a function similar to the forEach function .}

If the browser does not support forEach, We need to write a foreach function by ourselves. For the specific function body, see the following code:

Function (callback) {for (var I = 0; I <this. length; I ++) {callback. apply (this, [this [I], I, this]) ;}}

Therefore, the solution to IE8 does not support forEach should be as follows:

If (typeof Array. prototype. forEach! = 'Function') {Array. prototype. forEach = function (callback) {for (var I = 0; I <this. length; I ++) {callback. apply (this, [this [I], I, this]) ;}};}

If you use the js plug-in, you only need to put the changed code at the beginning of the plug-in code. In this way, all browsers support js foreach.

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.