admin管理员组文章数量:1327926
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 oldfor
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
1 Answer
Reset to default 7You 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 yourfor (xy of x)
loop. Doing so does not involve a lot of effort, and as I noted in a ment yourfor
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.
本文标签: javascriptHow do trycatch statements really work in IEStack Overflow
版权声明:本文标题:javascript - How do try...catch statements really work in IE? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742254237a2441333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论