Detecting user language in IE - javascript

I have some code that checks the user's language. If the user is German they get sent to "de.html" otherwise they're sent to "en.html". There is also a string that checks if the user is going to the editor, ('/?edit'), and if so nothing happens. This code works fine, however, it doesn't work in IE. Any ideas?
var lang = window.navigator.language;
var userLang = window.navigator.userLanguage;
if (!window.location.href.includes('/?edit')) {
if (lang == "de" || userLang == "de") {
window.location.href = window.location.href + "de";
} else {
window.location.href = window.location.href + "en";
}
}

includes() is not supported in Internet Explorer (or Opera). You need to use indexOf() instead of includes()
if(window.location.href.indexOf('/?edit') === -1)
//check if `window.location.href` do not include `/?edit`

Related

After if statment nothing will fire in javascript

There is very strange. It looks like after the If statement, the script it stop. The page has a javascript function is called by clicking the button. If the browser on IE, I will do something. Otherwise I close the page. I put alert statement to test it. However the alert has never fired. Would someone tell me what's wrong will my code.
There is my button call the function:
<input class="btn" id="btnClose" onclick="javascript:openFile();" type="button" value="Close" name="btnClose" />
There is the javascript function:
function openFile() {
var url = 'file://' + document.getElementById("hdURL").value;
//alert('Open File' + url);
var location = document.getElementById("hdURL").value;
////http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
if ((/*#cc_on!#*/false || !!document.documentMode) ||(!isIE && !!window.StyleMedia))
{
alert('IE');
//do something
window.self.close();
}
alert('test'); //never fire
closeWindow();
}
First off, going to give credit to Amy in the comments on the question for realizing that your isIE is not defined.
Take a look at the accepted answer to the question you referenced in your code. It says the following: (I've added an arrow to show the use of isIE)
// Internet Explorer 6-11
var isIE = /*#cc_on!#*/false || !!document.documentMode;
// |________
// |
// Edge 20+ V
var isEdge = !isIE && !!window.StyleMedia;
Notice how their example for detecting edge references the variable for detecting IE, isIE. In your example, you don't have var isIE, and so this is coming back as undefined - the code fails.
Why does it work in IE but not Firefox/Chrome?
JavaScript will not evaluate the second condition of an OR if the first is true - this is known as Short Circuit Evaluation.
When using IE, the first condition evaluates to true. This means that the second condition (and the syntax error therein) is ignored.
However, Chrome and Firefox get false for the first condition, and must evaluate the second. Once they get to the undefined variable, an error will be thrown.
Solution:
// Internet Explorer 6-11
var isIE = /*#cc_on!#*/false || !!document.documentMode;
// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;
if (isIE || isEdge) {
//DO STUFF
}
it is always helpful to prepare jsifddle, so you can immediately test your solution and see errors.
It is also good to check browser console which shows errors
see working example
https://jsbin.com/sipukovono/edit?html,css,js,console,output
function openFile() {
var isIE=false; // temporary definition
var url = 'file://' + document.getElementById("hdURL").value;
//alert('Open File' + url);
var location = document.getElementById("hdURL").value;
if ((false || !!document.documentMode) ||(!isIE && !!window.StyleMedia))
{
alert('IE');
window.self.close();
}
alert('test'); //never fire
closeWindow();
}

how to differentiate between standard and quirks document mode for IE10

I have to check when a user has selected Quirks or Standard document mode from the developer tool for IE10. Using the below code, i always get the same value i.e. 10 for both the modes.
document.documentMode
Please let me know how could i discriminate between the two document modes in IE10. I am using javascript for the same.
I used the below code and every thing worked fine. This is working on all the IE versions (Tested and Verified :) ).
//Checks the document mode of the IE and displays an error if the doc mode is not supported
function CheckDocMode() {
//Get the browser name
var browserName = navigator.appName;
//Do not display the Div containing the error message
document.getElementById('DocModeError').style.display = 'none';
//Check if the browser is IE
if (browserName == "Microsoft Internet Explorer") {
//Get the IE version, document mode and complatibility mode
var IEVersion = GetIEVersion();
var IEDocMode = document.documentMode;
var IECompatibilityMode = document.compatMode;
//Confirm that the browser is IE8/9/10
if (IEDocMode != undefined) {
//Do not display the error message if the IE=10 and Doc Mode = Standard
if ((IEVersion == 10 || IEVersion == 9 || IEVersion == 8 || IEVersion == 7)
&& (IEDocMode == 10 && IECompatibilityMode == "CSS1Compat")) {
return;
}
//Display the error if the document mode is anything other than IE8 and IE9
if (IEDocMode != 8 && IEDocMode != 9) {
document.getElementById('DocModeError').style.display = 'block';
}
}
}
}
function GetIEVersion() {
var myNav = navigator.userAgent.toLowerCase();
return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}

Simple jQuery form Validation: Checking for empty .val() failing in ie9 due to placeholder polyfill

New Issue / Answer
I am using an HTML5 placeholder polyfill which is causing ie9 to set the input's placeholder text as the value. So while in HTML5 browsers the val attribute is empty, in the below code ie9 is seeing it as filled in.
I'm using jQuery to make sure all fields are filled in. This works fine in ie10, webkit, and mozilla but fails in ie9.
What am I doing wrong here, and why won't this code work in ie9?
Thank you!
$('#quoteform .button.next').on('click',function(){
var $me = $(this),
$myParent = $me.parent(),
$nextStep = $myParent.nextAll('fieldset:not(.disabled)').first(),
validate;
// If we're on step2, make sure all fields are filled in
if($me.is('#quote-step2 .button') || $me.is('#quote-step1 .button')) {
$me.parents('fieldset').find('input:visible').each(function(){
var $me = $(this),
myVal = this.value;
if(myVal === '') {
$me.parent().addClass('warning');
validate = false;
return;
} else {
if(typeof validate === 'undefined')
validate = true;
}
});
}
if(validate === false) {
alert('Please fill out all fields before continuing.');
return false;
}
switchView($nextStep, $myParent);
});
I ran into a similar issue and my workaround was to also test the value against the placeholder string in addition to testing for an empty value. Since the polyfill replaces the input's value with the placeholder string the empty value is the value of the placeholder in IE9.
$me.parents('fieldset').find('input:visible').each(function(){
var $me = $(this),
myVal = this.value,
myPlaceholder = $me.attr('placeholder');
if(myVal === '' || myVal === myPlaceholder) {
$me.parent().addClass('warning');
validate = false;
return;
} else {
if(typeof validate === 'undefined')
validate = true;
}
});

How to detect Firefox mobile with javascript?

I'm using the following code to detect whether the browser being used on my mobile site matches a certain crieteria:
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
if (isiPhone){ alert ('iphone');
but if I attempt to do this for Firefox / Mozilla, I can't get it to work. I've tried:
var isFirefox = navigator.userAgent.match(/Mozilla/i != null);
and
var isFirefox = navigator.userAgent.match(/Firefox/i != null);
I visited whatismyuseragent.com and got the following:
Mozilla/5.0 (Android;Linux armv7l; rv6.0) Gecko/20110811 Gecko Firefox/6.0 Fennec/6.0
Any idea how I properly detect this? I need to write some firefox specific code.
You can use the navigator.userAgent to detect the browser and navigator.platform to detect the current platform.
To Detect Firefox:
var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
To Detect Android:
var is_android = navigator.platform.toLowerCase().indexOf("android") > -1;
To Detect Both:
if(is_firefox && is_android)
//Do Work
I would recommend using something like modernizr to avoid browser detection and focus on feature detection.
var isFirefox = /Android.+Firefox\//.test(navigator.userAgent);
The mobile version of Firefox is Fennec, so just search for that:
var is_Firefox = navigator.userAgent.toLowerCase().indexOf('fennec') > -1;
None of the above functions were working for me, specifically buriwoy was detecting either android or firefox, this version of his function works:
function detectAndroidFirefox () {
var agent = navigator.userAgent.toLowerCase();
if(agent.indexOf('firefox') >= 0){
if(agent.indexOf("android") >= 0){
return true;
} else{
return false;
}
} else{
return false;
}
}
you can check from user agent if it's contain firefox or android, for this maybe you need some code with regex
Rion's answer doesn't work (at least anymore), because navigator.platform doesn't return Android, it returns Linux.
I wrote a function which seems to work:
function detectAndroidFirefox () {
var agent = navigator.userAgent.toLowerCase();
return (agent.indexOf('firefox') + agent.indexOf("android")) >= 0;
}
Thought maybe someone will need this.

How redirect cancel function in Javascript

I'm not a programmer. I don't want to protect with a strong secure code my page. I just need one option I'm missing in my code and can't figure out how to add it.
<script language="Javascript">
<!--
var password = "lala"
var x = prompt("","")
if (x.toLowerCase() == password) {
location = "http://google.com";
}
else {
alert("Fail")
location = "http://facebook.com"
}
//-->
</script>
As you can see it's so dump but I need it. When I press Cancel button instead of writing true or false text, website still opens. I want to include in this script cancel button function (control it, you know) whitch would redirect to another website if press on it (as it is with true or false functions). I don't want to creat a special button or an input for it.
Update: I would like to include this script in a page which i am redirecting to. Could anyone tell me:
1. How can i modify this script to make it work only once?
2. Is it anything to do with browser's cookies?
p.s. Done :)
If the user presses cancel, prompt will return null. So do like this:
if(x == null) // Cancel
{
alert('Cancel');
}
else if (x.toLowerCase() == password) { // Correct password
location = "http://google.com";
}
else { // Wrong password
alert("Fail")
location = "http://facebook.com"
}
However, I'm not sure if all browsers will return null when the user presses cancel. (I have tested in Opera)
Try
var x = prompt("","");
if( x == null || x == '')
return;
if (x.toLowerCase() == password) {
location = "http://google.com";
}
else {
alert("Fail")
location = "http://facebook.com"
}
To redirect your browser to a URL use following snippet in your Javascript:
top.location.href = "http://google.com";

Categories