Detect browser user with javascript [duplicate] - javascript

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.

Related

Unsafe assignment to innerHTML [duplicate]

This question already has an answer here:
Best way to purge innerHTML from a Firefox Extension
(1 answer)
Closed 2 years ago.
I was trying to get an add on for Firefox signed by Mozilla so I could use it on the stable version of firefox and I'm getting this validation issue.
Can someone help me understand what it is?
Unsafe assignment to innerHTML
Warning: Due to both security and performance concerns,
this may not be set using dynamic values which have not been adequately sanitized.
This can lead to security issues or fairly serious performance degradation.
datetime.js line 4 column 5
function updateClock(){
var doc=window.content.document
var dt = new Date();
doc.getElementById("datetime").innerHTML = dt.toLocaleTimeString();
}
setInterval(updateClock, 0);
dt.toLocateTimeString() return a String instead of HTML.
Instead of, use innerText or textContent:
doc.getElementById("datetime").innerText = dt.toLocaleTimeString();
doc.getElementById("datetime").textContent = dt.toLocaleTimeString();

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.

javascript events not working with Chrome Extension , why ! :/ [duplicate]

This question already has answers here:
Port error while changing chrome extension from manifest v1 to v2
(2 answers)
Closed 8 years ago.
HTML // code
input id ="submit_btn" type="submit" value="find" onclick="goto();"
Javascript / code
function goto()
{
if (document.getElementById("s_keyword").value != "") {
var url = ("https://www.youtube.com/results?search_query=" + document.getElementById("s_keyword").value);
var site = window.open(url, '_blank');
site.focus();
}
};
it never enters into goto function !
Chrome extensions don't support inline events. Add the event listener in your JavaScript, and it'll be fine:
document.getElementById('submit_btn').addEventListener('click', goto);
You may want to avoid using goto as your function's name too. It may be a reserved keyword.

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.

Why do I get the error IE Trim method isn't supported? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.trim() in JavaScript not working in IE
i have the following code but in IE it says that the trim method is not supported.
for (var i = 0; i < here.length; i++) {
if ($(here[i]).html().trim() == "")
$(here[i]).parent().find('#content').css('width', '656px'),
$(here[i]).parent().find('#content p').css('width', '430px');
if ($(another[i]).html() != null) {
if ($(another[i]).html().trim() == "")
$(another[i]).parent().parent().css('display', 'none'),
$('#sidemenu .heading').css('background', 'none');
}
}
How do I fix it?
Older versions of Browsers (e.g. IE before version 9) don't have a trim method for strings implemented in their Javascript. You have these options:
Use JQuery.trim instead
or define trim yourself - see the solutions pointed out in the answer to this question.

Categories

Resources