最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do try...catch statements really work in IE? - Stack Overflow

programmeradmin6浏览0评论

I have a foreach() loop in this function and by searching the internet I know, that for each loop doesn't work in IE. To save my time I simply put a: try {} catch {} around it, but IE still reminds me, by function call, that there is an error.

Why does IE 11 work ? Code:

Code:

function classAndIdSpy() {

var req = new XMLHttpRequest();
var x = document.getElementsByClassName("spy");
var page_you_on = window.location.href.split("/");
var json56 = '{ "div_ids_classes" : [{"PAGE":"'+page_you_on[page_you_on.length-1]+'"},';

try {
   // Block of code to try. 
for(var xy of x) { 

json56 += '{"SPY_ID":"'+xy.id+'","CLASS":"'+xy.parentNode.className+'","ID":"'+xy.parentNode.id +'"}';

json56 += (x.length-1 > [].indexOf.call(x, xy)) ? ',' : '';

}
json56 += ']}';

req.open('POST', '../admin/id_class_colect.php', true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send("json2="+json56);

var status;
req.onreadystatechange = function() {//Call a function when the state changes.
    if(req.readyState == 4 && req.status == 200) {
        status = req.responseText;        
        console.log("Send ajax. Colected Id and Class information of content DIVs into database. Status:"+status);         
    }}
}
catch(err) {
      console.log("Your browser doesn't support ID and Class information of content DIV into database. Please use another browser.");
       return false;   
   } 
} //End of ClassAndIDspy

I get the following error in the console: SCRIPT1004: Expected ';' (The function works fine in Firefox, and I don't think there is a semicolon missing)

And, this is the part I would like Internet Exporer to ignore: Code:

for(var xy of x) { 

json56 += '{"SPY_ID":"'+xy.id+'","CLASS":"'+xy.parentNode.className+'","ID":"'+xy.parentNode.id +'"}';

json56 += (x.length-1 > [].indexOf.call(x, xy)) ? ',' : '';

}

Thank you for any help !

I have a foreach() loop in this function and by searching the internet I know, that for each loop doesn't work in IE. To save my time I simply put a: try {} catch {} around it, but IE still reminds me, by function call, that there is an error.

Why does IE 11 work ? Code:

Code:

function classAndIdSpy() {

var req = new XMLHttpRequest();
var x = document.getElementsByClassName("spy");
var page_you_on = window.location.href.split("/");
var json56 = '{ "div_ids_classes" : [{"PAGE":"'+page_you_on[page_you_on.length-1]+'"},';

try {
   // Block of code to try. 
for(var xy of x) { 

json56 += '{"SPY_ID":"'+xy.id+'","CLASS":"'+xy.parentNode.className+'","ID":"'+xy.parentNode.id +'"}';

json56 += (x.length-1 > [].indexOf.call(x, xy)) ? ',' : '';

}
json56 += ']}';

req.open('POST', '../admin/id_class_colect.php', true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send("json2="+json56);

var status;
req.onreadystatechange = function() {//Call a function when the state changes.
    if(req.readyState == 4 && req.status == 200) {
        status = req.responseText;        
        console.log("Send ajax. Colected Id and Class information of content DIVs into database. Status:"+status);         
    }}
}
catch(err) {
      console.log("Your browser doesn't support ID and Class information of content DIV into database. Please use another browser.");
       return false;   
   } 
} //End of ClassAndIDspy

I get the following error in the console: SCRIPT1004: Expected ';' (The function works fine in Firefox, and I don't think there is a semicolon missing)

And, this is the part I would like Internet Exporer to ignore: Code:

for(var xy of x) { 

json56 += '{"SPY_ID":"'+xy.id+'","CLASS":"'+xy.parentNode.className+'","ID":"'+xy.parentNode.id +'"}';

json56 += (x.length-1 > [].indexOf.call(x, xy)) ? ',' : '';

}

Thank you for any help !

Share Improve this question edited Jul 4, 2022 at 13:21 Beauty Of Salvation In Christ 211 silver badge13 bronze badges asked Mar 5, 2017 at 19:11 user7582130user7582130 3
  • 1 Please format your code. – James Monger Commented Mar 5, 2017 at 19:28
  • Frankly, why do you bother with a for (xy of x) loop here? You're using the index of the element, in [].indexOf.call, so I don't see why you don't just use a plain old for loop here. – Luke Woodward Commented Mar 5, 2017 at 21:09
  • if you want to get a job somewhere and keep it, learn to use proper indentation, this code is unreadable – OZZIE Commented Jan 4, 2019 at 6:00
Add a ment  | 

1 Answer 1

Reset to default 7

You appear to have misunderstood the point of try-catch.

try and catch are used to handle exceptions that arise when your JavaScript code runs. Your code doesn't get to run in IE because it fails to pile. There's nothing try and catch can do about pilation failures.

As I see it, your options are:

  • Use Babel or some other transpiler to convert modern JS code to JS code that will run in IE.
  • Use a 'plain' for loop instead of your for (xy of x) loop. Doing so does not involve a lot of effort, and as I noted in a ment your for loop is using the index of the element within the array anyway.

I strongly remend you give up on your idea of getting IE to ignore a block of code.

发布评论

评论列表(0)

  1. 暂无评论