JavaScript browser agent redirect - javascript

I've got this little bit of code on my webpage header to redirect the user to a different page if they are using an iPhone:
<script type="text/javascript">
if ((navigator.userAgent.indexOf('iPhone') != -1) ||
(navigator.userAgent.indexOf('iPod') != -1)) {
document.location = "iPhone.aspx"; }
</script>
Is there an easy way to 'reverse' this code, so anyone landing on the iPhone page from ANY other browser will be redirected back to the home page?
Many thanks.

Simple solution. Replace your existing one with this.
if(!/(iphone|ipod)/i.test(navigator.userAgent)) {
window.location = "Desktop.aspx";
}
Updated since its only for iPhone page.

To reverse? Replace != -1 with == -1
Edit:
Suppose you mean something like this:
if ((navigator.userAgent.indexOf('iPhone') != -1) ||
(navigator.userAgent.indexOf('iPod') != -1)) {
document.location = "iPhone.aspx"; } else {
document.location = "notiPhone.aspx";
}

(navigator.userAgent.indexOf('iPhone') == -1 means there is no such string "iPhone" in the userAgent. So when you change from != to ==, you need to change || to && as well.
if ((navigator.userAgent.indexOf('iPhone') == -1)
&& (navigator.userAgent.indexOf('iPod') == -1)) {
document.location = 'default.aspx';
}

Related

javascript if condition trouble

I am trying to write this condition in javascript:
if(window.location.pathname != '/our-communities.php' && (window.location.pathname == '/upcoming-communities.php' && window.location.search != '')){
What I am trying to say is if the page is not our-communities.php and if the page is not upcoming-communities.php and window.location.search is not blank then run the code.
So this condition should run on every page except for our-communities.php but can run on upcoming-communities.php only if window.location.search is blank.
if (window.location.pathname != "/our-communities.php" || (window.location.pathname == "/upcoming-communities.php" && window.location.search == "")) {
}
One thing to note, if the page is "/upcoming-communities.php" then the first condition will always be true meaning the second OR statement is irrelevant, if you want to enforce that then extra conditionals are needed.
if ((window.location.pathname != "/our-communities.php" && window.location.pathname != "/upcoming-communities.php") || (window.location.pathname == "/upcoming-communities.php" && window.location.search == "")) {
}

javascript Logical Operators don't do function when page equals to X

I dont want to do the if statement when on pageAge OR ON pageMore. When i go to pageAge it works it doesn't execute the script but when I go to pageMore it does. I'm not sure what operator to use in this situation. When I put pageMore before the || it works on that page but not on the othe one.
if ( top.location.pathname != pageAge || pageMore) {
//if not verified go to connect
$("body").css("display", "none");
if (age === null && top.location.pathname != pageConnect) {
window.location.href = pageConnect;
}
//if to young go to age page while cookie is found (1day)
if (age == toYoung) {
window.location.href = pageAge;
}
//if already verified go to like page.
if (age == legal && top.location.pathname === pageConnect) {
window.location.href = pageLike;
}
}
It should be:
if ( top.location.pathname != pageAge && top.location.pathname != pageMore)

Javascript if (window.location.href) adding two urls

Very simple question but can't find the answer. Can you put two url's inside
if (window.location.href
here is mine:
if (window.location.href == 'http://example.example.com/support/default.asp') {
}
I need to add a second link... so i only want a div to load from that page and one more page.
It should be as simples as
if (window.location.href == 'http://example.example.com/support/default.asp' ||
window.location.href == 'http://secondurl.com') {
}
unless I misunderstood the problem.
You can use || (or) in if loop.
if (window.location.href == 'http://example.example.com/support/default.asp'
|| window.location.href == 'http://example2.com') {
}

Unable to redirect to iPhone, iPad using JavaScript

I need to redirect mobile users to using respective device i.e. iPhone, iPad or Android.
For Android following javascript code redirects it properly.
if ((navigator.userAgent.indexOf('Android') != -1)) {
document.location = "http://www.cnn.com";
}
But i tried following code for iPhone:
if ((navigator.userAgent.indexOf('/iPhone/i') != -1) || (navigator.userAgent.indexOf('/iPod/i') != -1)) {
document.location = "http://www.ebay.com";
}
or
if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
document.location = "http://www.ebay.com";
}
None of above code redirects website in iPhone or iPad.
I can't figure out why.
Please help or suggest.
I have been able to use this Javascript to get redirection in Android, iOS and BB.
<html>
<script type='text/javascript'>
function detectDevice()
{
var ua = navigator.userAgent;
if (ua.indexOf("BlackBerry") >= 0) {
window.location.replace("http://www.cnn.com");
}
else if (ua.indexOf("iPhone") >= 0){
window.location.replace("http://www.ebay.com");
}
else if (ua.indexOf("iPad") >= 0){
window.location.replace("http://www.ebay.com");
}
else if (ua.indexOf("Android") >= 0){
window.location.replace("http://www.google.com");
}
}
</script>
<body onload="detectDevice()">
</body>
</html>

If statement with url plus universal variable

I'm trying to transform a blog on blogger into a website. In order to have a static home page I am using the Javascript code below to see if the user is on the home page if they are then it will hide the post section and display a home page "gadget". Is anything supposed to match anything?
document.onload = hidepage();
function hidepage () {
if (window.location == "http://website.blogspot.com/" || window.location == "http://website.blogspot.com/?zx=" + ANYTHING) {
//Checks to see if user is on the home page
$(".hentry").hide(); //Hide posts
$(".hfeed").hide(); //Hide posts
}
else {
$("#HTML2").hide(); //hide gadget
}
$(".post-title").hide(); //Hide post titles
}
Based on what you're saying I think you want to change the if condition to:
if (window.location.href === "http://website.blogspot.com/" ||
window.location.href.indexOf("http://website.blogspot.com/?zx=") > -1)
You could also shorten this to:
if (window.location.href === "http://website.blogspot.com/" ||
window.location.href.indexOf("/?zx=") > -1)
Note that I've changed your == to === as the latter is a literal comparison.
Just use String.indexOf in the second half of the if expression.
var url = window.location.href;
if (url === "http://website.blogspot.com/" || url.indexOf("http://website.blogspot.com/?zx=") === 0) {
// do stuff
}

Categories