jQuery Dynamic Element Error in IE7/8 [duplicate] - javascript

This question already has answers here:
Expected identifier, string or number
(4 answers)
Closed 8 years ago.
When creating dynamic elements in jQuery 1.7.2, I found that this code works fine in Safari, Chrome, Firefox, and IE9/10. However, in IE7/8 this code yields this error message:
SCRIPT1028: Expected identifier, string or number
Here is the element creation code:
$("<span></span>", {
text: "Please Specify: ",
class: $(this).attr("id")+"other" <-- error points here
});
Why is declaring a class causing this issue?

Because in ECMAScript3 (current supported version is ES5), "class" is a reserved word, you must add quotes around property name class for it to be IE7/8 compliant. Adding quotes fixes the issue and the code now works in every browser I have tested.
$("<span></span>", {
text: "Please Specify: ",
'class': $(this).attr("id")+"other"
});

Related

Bootstrap Javascript error: Syntax error, unrecognized expression: .bs-docs-container [href=#] [duplicate]

This question already has answers here:
Error while query selector on numeric value
(2 answers)
Closed 4 years ago.
I inherited this project built in Jekyll with Bootstrap CSS. The Javascript console keeps indicating the following error:
Syntax error, unrecognized expression: .bs-docs-container [href=#]
Pretty sure this js is causing the error:
a(".bs-docs-container [href=#]").click(function(a){
a.preventDefault()
})
But it's been a long time since I've done any javascript and I am not sure how to debug this. This looks like it is simply canceling the event if the ` when it is within a div referencing .bs-docs-container.
Any help would be appreciated.
a(".bs-docs-container [href='#']").click(function(a){
a.preventDefault()
})
Mind the single quotes around the hash
You need to quot hash mark
a(".bs-docs-container [href='#']")

Checking if template literals are available in script [duplicate]

This question already has an answer here:
creating an error message if browser does not support ES6 Template Literals
(1 answer)
Closed 4 years ago.
I'm trying to find a way to check if template-literals in JS are supported in the given browser. The idea is to pass them different scripts created by Babel if the browser doesn't support ES6 features. So I'm trying:
function check_es6() {
try { `foo` }
catch (e) { return false; }
return true;
}
alert(check_es6());
However, on my old iPhone I get:
Invalid character On line 141 of
https://m.xxx.org/2018/js/app.js on 1 page.
Seen in Mobile Safari 7. Last seen 20 minutes ago. Occurred once so far. Occurred before page load.
Line 141 is:
try { `foo` }
Is there a better way to do what I need?
OK, so managed to work it out myself :)
try { eval("`foo`"); }
This correctly shows "false" now when the browser doesn't support template literals, and "true" in later browsers that do.

Javascript Arrays - Ignoring Extra Index Values [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 8 years ago.
Using Javascript in Firefox, Safari and Chrome (didn't try IE), the following strange behavior happens:
data = new Array(1,3,5,7,9);
data[1,7,3] = 88;
alert( data ); // displays: 1,3,5,88,9
So apparently the value at index [3] has been changed (other tests show that it actually has changed data[3]).
The second command does not generate an error. It does not even generate a warning in NetBeans, nor an error when displayed in a browser.
I explored this a bit further. It appears that :
data[1,7,null,NaN,4,3]
also gets interpreted as data[3] -
this also works with other values than 3.
The last value in the list is used and the rest are ignored.
Does this behavior have some sort of meaning or purpose, or is it just an unexpected fault in the parser?
I was unable to find any documentation or explanation of this behavior/syntax.
You're using the comma operator.
<expr1>, <expr2>, <expr3>, ...
is an expression that evaluates each expression from left to right, and returns the value of the last one. So 1,7,3 evaluates to 3. So
data[1,7,3] = 88;
is equivalent to:
data[3] = 88;

Javascript error "Uncaught TypeError: Object #<HTMLInputElement> has no method 'removeNode'" [duplicate]

This question already has answers here:
What is the equivalent of IE's removeNode
(2 answers)
Closed 9 years ago.
I am getting JavaScript error on Chrome like
Uncaught TypeError: Object #<HTMLInputElement> has no method 'removeNode'
and my code is
if (document.form["act[" + actArry["'" + i + "'"][i] + "]"] != undefined)
document.form["act[" + actArry["'" + i + "'"][i] + "]"].removeNode(true);
and a value which is stored in that input element is
<input type="hidden" name="act[1]" value="7813e7-true">
Actually I want to remove -true when checkbox is unchecked.
This is working properly in IE but not in Google Chrome.
Can anybody tell me what is the issue and which should be the common method in both IE and Chrome? Is there an alternative in jQuery?
removeNode() is an IE only method. It will not work in other browsers.
You can do a removeChild() on the parent node to achieve the same along with being cross browser.
i.e.:
if (node.parentNode)
node.parentNode.removeChild(node);
Ref: http://www.sitepoint.com/forums/showthread.php?126312-Mozilla-equivalent-for-IE-s-removeNode()
You can use removeChild, it works in most browsers.

document.createElement throwing error in IE 8. This command is not Supported ERROR [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript doesn't work in IE8
I have the following code
var ind=1;
try
{
rdo = document.createElement('<input type="radio" name="radioOptions" />');
}
catch(err)
{
rdo = document.createElement('input');
}
rdo.setAttribute('type','radio');// error
rdo.setAttribute('name','radioOptions');
rdo.id = 'radioOption_'+ind;
rdo.value = ind;
After a thorough checkup this line is throwing error on IE 8
rdo.setAttribute('type','radio')
and a strange fact is that when it is on the local system its not doing that.
I am dynamically adding this radio input to the form. And the Doc type i have set to
<!doctype html>
Any Idea what should work for all Browsers including the ASS HOLE IE
You can not change the type of input elements in IE with setAttribute(). You could try with rdo.type = 'radio' (which should work) or (ugh) innerHTML.
Also, document.createElement() is used with the element's name, i.e. input. It is not like $() in jQuery or similar libraries.

Categories

Resources