javascript problems for IE and FF [duplicate] - javascript

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.

Related

document.getElementById(...) is null in Chrome but working on older browsers like Old IE [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 4 years ago.
I am getting an error as in the title when I open the console in Chrome and Firefox. However, everything is working fine on older browser like older IE versions. You can check this image.
I am attaching the script:
function MoveNext()
{
var qno = document.getElementById("txtQueNo").value;
var qcode = document.getElementById("txtQueCode").value;
var quetot = document.getElementById("quetot").value;
var que_avl = document.getElementById("que_avl").value;
var stdid = document.getElementById("txtStdID").value;
var oltid = document.getElementById("txtOLTID").value;
var ans = document.getElementById("txtAnswer").value;
var time = document.getElementById("disp").value;
I'm adding the full script for reference.
The issue is on the last line of the above code.
Thank you.
The only occurrence of "disp" in your php is as a name and not an ID (id="txt").
You can use document.getElementsByName("disp")[0].value instead. This will of course return an array of all elements with name="disp" but as you only have one in the php provided, this should be the correct assignment for the 0 based index.

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.

Detect browser user with javascript [duplicate]

This question already has answers here:
How to detect Safari, Chrome, IE, Firefox and Opera browsers?
(30 answers)
Closed 5 years ago.
I'm new in this board but I have read you for years. :)
I'm trying to learn to code, but I'm in the first steps yet. However, I'm going to use a script to detect the user's browser.
This work with all the mainly browser (I tried it on chrome, firefox and IE), but it doesn't work with Edge. How can I fix it? Thank you!
<p style="text-align: center;">Hi user, you're using:
<script>
var ba = ["Chrome","Firefox","Safari","Opera","MSIE","Trident","Edge"];
var b, ua = navigator.userAgent;
for(var i=0; i < ba.length; i++){
if( ua.indexOf(ba[i]) > -1 ){
b = ba[i];
break;
}
}
if(b == "MSIE" || b == "Trident" || b == "Edge 20+"){
b = "Internet Explorer";
}
document.write(b + " browser");
</script>
As you are just starting out, most browser have developer tools, which if you look at the network setting you can see the request headers for each item requested. This will generally show you the user agent string, but as highlighted in comments, this can be spoofed / changed quite easily. You can use the browser tools to aid you when writing your client side javascript, etc. Good luck and enjoy.

JavaScript:: Is innerText safe to use? [duplicate]

This question already has answers here:
'innerText' works in IE, but not in Firefox
(15 answers)
Closed 9 years ago.
I've heard that using el.innerText||el.textContent can yield unreliable results, and that's why I've always insisted on using the following function in the past:
function getText(node) {
if (node.nodeType === 3) {
return node.data;
}
var txt = '';
if (node = node.firstChild) do {
txt += getText(node);
} while (node = node.nextSibling);
return txt;
}
This function goes through all nodes within an element and gathers the text of all text nodes, and text within descendants:
E.g.
<div id="x">foo <em>foo...</em> foo</div>
Result:
getText(document.getElementById('x')); // => "foo foo... foo"
I'm quite sure there are issues with using innerText and textContent, but I've not been able to find a definitive list anywhere and I am starting to wonder if it's just hearsay.
Can anyone offer any information about the possibly lacking reliability of textContent/innerText?
EDIT: Found this great answer by Kangax -- 'innerText' works in IE, but not in Firefox
It's all about endlines and whitespace - browsers are very inconsistent in this regard, especially so in Internet Explorer. Doing the traversal is a sure-fire way to get identical results in all 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