user2610063
user2610063

Reputation: 11

JavaScript forEach loop works in all webbrowser but Internet Explorer

I've managed to get the loop working in all browsers apart from Internet Explorer (which doesn't seem to support forEach).

The JavaScript cpde:

function validate() {
    var msg = '';
    var i = 0;

    arr.forEach(
        function validateinfo(){
            if (getRBtnName('yesNo_' + i + '_0' == "" && 'yesNo_' + i + '_0') == "") {
                msg = 'Please select yes/no for all users'
            }
            if (msg == '') {
                return true;
            }
            is++;
        }
    )

    if (msg == '') {
        reloadpage();
    }

    if (msg != '') {
        alert(msg);
        return false;
    }
}


function reloadpage(){
    window.location.reload()
}

The array is being set in the PHP file rather than passed in. It's being set using:

<script type="text/javascript">
    var arr = <?php echo json_encode($arr) ?>;
</script>

Upvotes: 1

Views: 308

Answers (1)

VisioN
VisioN

Reputation: 145398

Just place this shim from MDN at the beginning of your scripts:

if ( !Array.prototype.forEach ) {
  Array.prototype.forEach = function(fn, scope) {
    for(var i = 0, len = this.length; i < len; ++i) {
      fn.call(scope, this[i], i, this);
    }
  }
}

Upvotes: 5

Related Questions