1

I would like to add a script that says if the window.location.origin is no equal to "http://www.mymobilesite.com" and the screen resolution is less than or equal to 600 then redirect to "http://www.mymobilesite.com"

I have been able to create each statement separately but still learning and when I go to combine them with && I get stuck. I can get it to redirect to mymobilesite.com but it still redirects even if the origin is mymobilesite.com Any help would be greatly appreciated! :)

Here is what I have been working on:

if ((window.location.origin != "http://www.mymobilesite.com") && ($(window).width() <= 600)) {
location.href = "http://www.mymobilesite.com"
}
Jenny
  • 851
  • 8
  • 22
  • sorry for the confusion, screen width. What I mainly need help with how to say if both of those statements are true then redirect to mymobilesite.com – Jenny Dec 07 '16 at 02:57
  • You did that part correctly in the code above, as far as I can tell. You might have a problem with the `www.` subdomain, though; see my answer below. – gyre Dec 07 '16 at 03:07

2 Answers2

1

Since it sounds like you're trying to determine whether a user is on a mobile device, why not find out the navigator's userAgent via regex:

var notSite = window.location.origin !== "http://razmobile.com.com";
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);

if (notSite && isMobile) {
  window.location.href = "http://www.mymobilesite.com";
}

source: https://stackoverflow.com/a/3540295/3046413

Community
  • 1
  • 1
Carl Edwards
  • 11,370
  • 7
  • 46
  • 88
  • Thank you for your help but this does not appear to work from my mobile. If I go to mymobilesite on a desktop then go to the site main site i am not redirected back to mymobilesite however if i go to main site on my mobile device i am taken to mymobilesite but when i click to go to the main site and click to go to the full version i am still redirected back to the mobile site. – Jenny Dec 07 '16 at 03:25
  • Thanks again for your work but the update does not appear to redirect at all. – Jenny Dec 07 '16 at 03:40
  • Let me start from scratch. What does my most recent code give you? – Carl Edwards Dec 07 '16 at 03:44
  • The latest code does not redirect from a mobile device, it does redirect from a desktop but when clicking to return to the PC view I am directed back to the mobile. – Jenny Dec 07 '16 at 03:51
  • i dont think this makes a difference but the mobile site is at http://razmobile.com so there is no www. – Jenny Dec 07 '16 at 03:53
  • Since your basically just testing whether you're on a mobile device, why not find navigator's user agent via regex. Let me know if my most recent answer works. – Carl Edwards Dec 07 '16 at 04:14
  • Thanks again, still seeing the same issue. I am taken to the mobile view when loading he page from my mobile but when i select PC view from the mobile view I am redirected back to mobile view. – Jenny Dec 07 '16 at 04:24
  • Are you on your mobile device when you select to view on desktop? – Carl Edwards Dec 07 '16 at 04:27
  • Yes, I am on a mobile – Jenny Dec 07 '16 at 15:06
  • So hypothetically it's doing what it's supposed to do: detecting if you're on a desktop/mobile device. Since you're on a mobile device it won't take you to desktop because you're not on desktop. Does that make sense? – Carl Edwards Dec 07 '16 at 15:17
0

In addition to the suggestions given already about using screen.width instead of jQuery, you could use regular expressions to make the redirect work whether or not you're using the www. subdomain of mymobilewebsite.com.

if (!/mymobilewebsite\.com$/.test(location.host) && screen.width <= 600) {
    location.href = "http://www.mymobilesite.com"
}
gyre
  • 14,437
  • 1
  • 32
  • 46