Checking if template literals are available in script [duplicate] - javascript

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.

Related

What is causing $ to be undefined? (JQuery) [duplicate]

This question already has answers here:
JSHint and jQuery: '$' is not defined
(9 answers)
Closed 6 years ago.
I am practicing my JQuery at the moment, and I cannot understand how to make this error message go away: '$' was used before it was defined. The code seems to work though. Nothing wrong with it.
I downloaded the latest version of JQuery, using a Safari browser, and writing my code in the Brackets text editor.
The Javascript code I have is:
$(document).ready(function () {
$("h2").mouseenter(function () {
$("h2").fadeTo('fast', 0);
});
$("h2").mouseleave(function () {
$("h2").fadeTo('fast', 1);
});
});
Below is a screenshot of the my code + error message
You have to tell JSLint it is a global. Add this comment to the top of your file:
/*global $, jQuery*/

How to detect whether browser supports :invalid pseudoclass? [duplicate]

This question already has answers here:
Test if a browser supports a CSS selector
(5 answers)
Closed 8 years ago.
I tried to use Modernizr, but it seems not to support this feature detection.
I also read that it is difficult or even inmpossible to access pseudoclasses from javascript, because they are not part of DOM. So, after surfing the web I found no relevant information.
I need an easy solution without the need to download heavy libraries.
Can anybody help me with this?
Thanks
Trap an error from querySelector or matches, which parses the selector and throws an error if it is not valid:
function invalid_pseudoclass_support () {
var support = true;
try {
document.querySelector(':invalid');
} catch (e) {
support = false;
}
return support;
}

jQuery Dynamic Element Error in IE7/8 [duplicate]

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"
});

javascript problems for IE and FF [duplicate]

This question already has answers here:
Do we have getElementsByClassName in javascript?
(5 answers)
Closed 8 years ago.
Could anyone please help me on this problem. The error message saying “object doesn't support this property of method, line 113, characters 5, url: http://xxxx.com/js/category.js” appears when I try to open some site by several IE browsers. I assume that the script called category.js has some problems, or is not just suited for IE browsers. The category.js is used to open and close several levels of category, and the line 113 of category.js is listed below. (No problem for chrome and safari, and some css problem for Opera. IE and FF has never been worked.)
function initCategoryList(){
var lv = 0;
while(document.getElementsByClassName("level" + (++lv)).length){ // line 113
for (var i = 0; i < document.getElementsByClassName("level" + lv).length; i++) {
setclickevent(document.getElementsByClassName("level" + lv)[i], lv, i);
}
}
}
Support for getElementsByClassName first appears in Internet Explorer in version 9. It will not work in earlier versions.
(It may also be disabled when viewing a page in compatibility mode).
If you need to support ancient browsers, there are plenty of pollyfills for getElementsByClassName available.

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