Script for browser detection and url linking - javascript

I'm new... I'm looking for a script that detects the browser and then a function that links to a specific url based on the browser being used. I'm stuck on how to combine two functions into one js file. Using the onclick method to call one of the two functions. Does this make sense? What are my options. I know userAgent is frowned upon, what are the work arounds?

Here is a script that can be modified very easily to include all modern browsers, this is not based on mouseover but is based on Browser detection and redirection depending on what browser.. if this is not what you are looking for please explain in more detail and either i or someone else will be able to help out better... Do you have example pages or code to work with?
version = parseInt(navigator.appVersion);
if (navigator.appVersion.indexOf('5.') > -1) {
version = 5
};
if (navigator.appVersion.indexOf('6.') > -1) {
version = 6
};
if (navigator.appVersion.indexOf('7.') > -1) {
version = 7
};
browser = 'OTHER';
if (navigator.appName == 'Netscape') {
browser = 'NS' + version;
}
if (navigator.appName == 'Microsoft Internet Explorer') {
browser = 'MSIE' + version;
}
if (navigator.appVersion.indexOf('MSIE 3') > 0) {
browser = 'MSIE3';
}
if (browser == 'NS5') {
browser = 'NS6'
};
if (browser == 'MSIE3') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'MSIE4') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'MSIE5') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'MSIE6') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'MSIE7') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'NS3') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'NS4') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'NS6') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'NS7') {
window.location = 'http://www.mywebsite.com'
}
if (browser == 'OTHER') {
window.location = 'http://www.mywebsite.com'
}
Thanks

Related

How to break if/else statement in JavaScript once a condition is true

So I have this basic JavaScript code to detect a user language and redirect to the proper page based on the browser language, the issue I am facing is that the if/else statement keeps going as an infinite loop and the browser keeps refreshing.
The code is set on a separate file and is included only in the en-US page, the code is a stand-alone, not using any function.
if(userLang == "en-US"){
window.location.href = "domainame.com/faq.html";
}
else if(userLang == "nl"){
window.location.href = "domainame.com/faq-de.html";
}
else if(userLang == "fr"){
window.location.href = "domainame.com/faq-fr.html";
}
else if(userLang == "es-ES"){
window.location.href = "domainame.com/faq-es.html";
}
else if(userLang == "ja"){
window.location.href = "domainame/faq-ja.html";
}
I expect the browser to check for the user language and redirect to the proper page, instead, the browser keeps refreshing.
You can add an extra check to only change the page if it is not the current page.
Something like this:
let lanPage = '';
if(userLang == "en-US"){
lanPage = "https://domainame.com/faq.html";
} else if(userLang == "nl"){
lanPage = "https://domainame.com/faq-de.html";
} else if(userLang == "fr"){
lanPage = "https://domainame.com/faq-fr.html";
} else if(userLang == "es-ES"){
lanPage = "https://domainame.com/faq-es.html";
} else if(userLang == "ja"){
lanPage = "https://domainame/faq-ja.html";
}
if(lanPage && lanPage !== location.href) {
location.href = lanPage;
}

iOS Safari standalone exception to external navigation

I have this code I grabbed somewhere to make my Standalone iOS Web-App stay within itself while navigating in the app:
(function(document, navigator, standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
if ((standalone in navigator) && navigator[standalone]) {
var curnode,
location = document.location,
stop = /^(a|html)$/i;
document.addEventListener('click', function(e) {
curnode = e.target;
while (!(stop).test(curnode.nodeName)) {
curnode = curnode.parentNode;
}
// Condidions to do this only on links to your own app
// if you want all links, use if('href' in curnode) instead.
if ('href' in curnode && (curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host)) && e.defaultPrevented !== true) {
e.preventDefault();
location.href = curnode.href;
}
}, false);
}
})(document, window.navigator, 'standalone');
This basically prevents the app from going in background and opening a link in Safari while in the App.
I need an exception to this, so that when I place a target="_blank" or a rel="external" attribute it actually has to open in Safari (and the WEb-App going to background).
I tried placing an if statement before line 15 like this:
(function(document, navigator, standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
if ((standalone in navigator) && navigator[standalone]) {
var curnode,
location = document.location,
stop = /^(a|html)$/i;
document.addEventListener('click', function(e) {
curnode = e.target;
while (!(stop).test(curnode.nodeName)) {
curnode = curnode.parentNode;
}
if (e.target.getAttribute('rel') == 'external') {
window.open("http://www.google.com");
} else {
// Condidions to do this only on links to your own app
// if you want all links, use if('href' in curnode) instead.
if ('href' in curnode && (curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host)) && e.defaultPrevented !== true) {
e.preventDefault();
location.href = curnode.href;
}
}
}, false);
}
})(document, window.navigator, 'standalone');
But does not work...
This is the solution:
(function(document, navigator, standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
if ((standalone in navigator) && navigator[standalone]) {
var curnode,
location = document.location,
stop = /^(a|html)$/i;
document.addEventListener('click', function(e) {
curnode = e.target;
while (!(stop).test(curnode.nodeName)) {
curnode = curnode.parentNode;
}
if (curnode.getAttribute('rel') == 'external') {
window.open("http://www.google.com");
} else {
// Condidions to do this only on links to your own app
// if you want all links, use if('href' in curnode) instead.
if ('href' in curnode && (curnode.href.indexOf('http') || ~curnode.href.indexOf(location.host)) && e.defaultPrevented !== true) {
e.preventDefault();
location.href = curnode.href;
}
}
}, false);
}
})(document, window.navigator, 'standalone');

Different redirects in function of multiple referer

Now I have this code of redirect in function of the referer:
document.addEventListener('DOMContentLoaded', function() {
console.log(isMobile);
var referrer = document.referrer;
if(referrer.indexOf('site1.com') !== -1 || referrer.indexOf('site2.com') !== -1) {
if(isMobile.phone) {
window.location = "http://www.landingphone.com";
console.log('Is phone');
} else if(isMobile.tablet) {
window.location = "http://www.landingtablet.com";
console.log('Is tablet');
} else {
window.location = "http://www.landingdesktop.com";
console.log('Is desktop');
}
} else {
window.location = "http://www.anotherlanding.com";
}
});
Its ok this code for redirect in function of the referer site1 and site2.com, but if I need redirect also another referer (for example site3.com) to another landing (for example www.landingphone2.com, landingtablet2.com and landingdesktop2.com). What I need add in the code? what i need modify?
Thank you very much.
You can try something like this, adding another else if statement as referred to in the comment to your answer.
document.addEventListener('DOMContentLoaded', function() {
console.log(isMobile);
var referrer = document.referrer;
if (referrer.indexOf('site1.com') !== -1 || referrer.indexOf('site2.com') !== -1) {
if (isMobile.phone) {
window.location = "http://www.landingphone.com";
console.log('Is phone');
} else if (isMobile.tablet) {
window.location = "http://www.landingtablet.com";
console.log('Is tablet');
} else {
window.location = "http://www.landingdesktop.com";
console.log('Is desktop');
}
} else if (referrer.indexOf('site3.com') !== -1) {
// Do your other redirects here
} else {
window.location = "http://www.anotherlanding.com";
}
});

(Only in IE) SCRIPT65535: Unexpected call to method or property access. main.js, line 152 character 28

After going through several stackoverflow posts regarding this error and none of them actually apply to my case, I am getting this error on a line in my js file in developer tools while debugging:-
var el = $("#" + tagName);
el.val(el.html(value).text()); //this is the one where debugger points to
This is the relevant code in js file:-
$.ajax({
type: "get",
url: "cgi-bin/system_status.cgi?tokn="+sessnID+",active="+tickle_activity+",id="+page_id+",sub="+sub_id+",tab="+tab_id,
dataType: "xml",
success: function (xml) {
errorCnt = 0;
$(xml).find('response').children().each(function () {
if (this.nodeName === "user_info") {
parseUserInfo(this);
}
else {
$(this).children().each(function () {
var tagName = this.tagName;
var value = $(this).text();
/* If this is a time block then do some translation */
if (tagName === "time") {
if (showTimeValues != undefined) { showTimeValues(this); }
}
else if (tagName === "satellite") {
if (parseSatDataXml != undefined) { parseSatDataXml(xml); }
}
else if (tagName === "ntpstatus") {
if (parseNtpDataXml != undefined) { parseNtpDataXml(xml); }
}
else if (tagName === "alarms") {
if (parseAlmDataXml != undefined) { parseAlmDataXml(xml); }
}
else if (tagName === "alarmSettings") {
if (parseAlmSettingDataXml != undefined) { parseAlmSettingDataXml(xml); }
}
else if (tagName === "status") {
setStatusTextColor(value);
setStatusButton(value);
$("#" + tagName).val(value);
}
else {
var el = $("#" + tagName);
el.val(el.html(value).text());//error thrown
}
});
}
});
Only in IE im having this issue, in all other browsers it works fine and i dont observe anything else other than that error in the debugging tools. I am using IE9 and latest jquery version 1.12.1
Actually, even though I was using IE9 in my developer tools there was an option which said Browser Mode: and it was set to IE7 standards. That is why I was getting this error. The moment i switched to IE9 standards (not the compatibility mdoe option). It all works as expected. If you are new to web development like me you might encounter this issue(not being aware that there are browser modes) as well.
So in summary,
In debugger tools (F12) -> Click on browser mode and choose IE9.
Dont choose the compatibility option.

Add to favourites/bookmarks bar in Safari (CMD +D)

From much research, I have found nothing that supports the idea that Safari even supports this feature. From how much API there is for Safari, I can't believe that they wouldn't allow this to be embedded with their browser.
If anyone has any ideas of how this can be achieved without using some horrible plugin that doesn't actually work, it would be greatly appreciated.
So far, I have taken care of the main browsers by using this:
$("#bookmark").click(function() {
var url = this.href;
var title = this.title;
if($.browser.mozilla) {
window.sidebar.addPanel(title, url,"");
} else if($.browser.msie || $.browser.webkit) {
window.external.AddFavorite(url, title);
if($.browser.safari) {
alert("Balls");
}
} else if($.browser.opera ) {
$(this).attr("href", url);
$(this).attr("title", title);
$(this).attr("rel", "sidebar");
$(this).click();
} else {
//alert("Please press CTRL+D and click the link to bookmark it in your browser.");
}
return false;
});
Unfortunately Safari does not allow you to add a bookmark through javascript (along with IE6/IE8) and possibly a few others. It's some sort of attempt at fighting off spam/unwanted websites adding bookmarks to your browser onload.
Try a script like this, it's pretty much all you can do...
$("a.bookmark").click(function(e) {
if ($.browser.opera == false) {
e.preventDefault();
var url = this.href;
var title = this.title;
if ($.browser.mozilla == true) {
window.sidebar.addPanel(title, url, '');
return false;
} else if($.browser.msie == true) {
window.external.AddFavorite( url, title);
return false;
} else {
alert('Please use CTRL + D to bookmark this website.');
}
}
});
Info from Apple forums (https://discussions.apple.com/thread/1364657?start=0&tstart=0)

Categories

Resources