sebarmeli / js-redirection-mobile-site Goto Github PK
View Code? Open in Web Editor NEWJS to handle the redirection to the mobile version of your site
License: MIT License
JS to handle the redirection to the mobile version of your site
License: MIT License
Pages should redirect to a mobile host keeping the same path
Add a parameter to allow redirection to site.com if user tries to access to m.site.com from not-mobile device.
Please add the Microsoft Surface (that no one is going to buy) with for the agent = "Mozilla/4.0 (Compatible; msie 7.0; windows nt 6.2; arm; trident/6.0; touch; .net4.0e; .net4.0c; tablet PC 2.0; Version)"; // Microsoft surface.
We should be safe to add "tablet" in the agent match pattern.
I was getting a continuous redirect to my tablet_host, because you where checking the referrer to only the mobile_host. The fix was move the isUATablet test before setting the cookies/session and the change the test to
(isUAMobile && document.referrer.indexOf(mobile_host) >= 0 )
|| (isUATablet && document.referrer.indexOf(tablet_host) >= 0) || ....
Oh .. your setting either the session OR cookie variable. So the final test for the redirection should be "&& !isCookieSet && !isSessionStorage" not !(isCookieSet || isSessionStorage).
William Choy
NorthPoint Solutions
PS: Here is the patch (Sorry for the funky formatting):
105a106,113
// Check if the device is a Tablet such as iPad, Samsung Tab, Motorola Xoom, Amazon Kindle or MS Surface
if (!!(agent.match(/(iPad|SCH-I800|xoom|NOOK|silk|kindle|GT-P7510|tablet)/i))) {// Check if the redirection needs to happen for tablets isUATablet = (config.tablet_redirection === TRUE || !!config.tablet_host) ? true : false; isUAMobile = false;
}
109c117
< if (document.referrer.indexOf(mobile_host) >= 0 || queryValue === FALSE ) {
if ((isUAMobile && document.referrer.indexOf(mobile_host) >= 0 ) || (isUATablet && document.referrer.indexOf(tablet_host) >= 0) || queryValue === FALSE ) {
129,136d136
< // Check if the device is a Tablet such as iPad, Samsung Tab, Motorola Xoom or Amazon Kindle
< if (!!(agent.match(/(iPad|SCH-I800|xoom|NOOK|silk|kindle|GT-P7510)/i))) {
<
< // Check if the redirection needs to happen for tablets
< isUATablet = (config.tablet_redirection === TRUE || !!config.tablet_host) ? true : false;
< isUAMobile = false;
< }
<
138c138< if ((isUATablet || isUAMobile) && !(isCookieSet || isSessionStorage)) {
if ((isUATablet || isUAMobile) && !isCookieSet && !isSessionStorage) {
Change name to "param" to be "localstorage_item"
Noticed with the update last week to Android 4.0.4 that the script is not working on the noredirection call. It will redirect to the mobile site fine but not allow the override to the full site work.
Stop the redirection if there is a parameter int he URL
Make the function more flexible
Hi, i just used this new version of redirection script. It seems that the redirection for samsung galaxy tab is still not working. Any advice?
Create a new function "redirection_mobile_testable" where you mock those parameters
Move the function from window scope
Apologies, doesn't seem to be a way to delete thread
At the moment the prefix ("m" default one) is prepended to the url, I'd like to define the position of the "prefix", so it could be possible something like redirecting "en.domain.com" to "en.m.domain.com".
A week or so ago, I was looking for a solution to redirect our users from our classic site to our mobile site if they were on a mobile device. I found your JS and it appeared to work perfectly for my needs.
I had originally tested the script using the following devices LG Optimus G with Android 4.1, iPhone4 with most recent version OS, iPad with an unknown OS version, and a Samsung Galaxy Note2 with unknown OS version. They had all worked perfectly with in our internal network and without.
But a week later, I’m getting reports that some of our users on our internal wireless network are getting stuck in an endless “loading loop”, where it never actually redirects to the mobile site.
So far we’ve noted the following devices iPad with the most recent version OS, a Samsung Galaxy S1 running Android 2.3 and a Motorola Xoom running Android 4.1. All are using the browsers that came with their OS.
I was concerned that the user-agents weren't set up correctly. (I’m totally new to this mobile stuff and I’m hardly a JS guru. I’m mainly a designer!) But I was having a hard time interpreting the code that I think is defining this information. Line 102? A fellow developer recommended keeping my User-Agent Sniffing up to date using the site - http://detectmobilebrowsers.com that is noted in the script. So I did that.
This is a demo of how I set it up:
http://codepen.io/downcasteyes/pen/rztne
Now the endless loading loops have ceased, but so has all redirections on all devices we've tested. How would updating the user-agents affect the redirection like that? Is there something else in my version of the code that I need to look at?
There seems to be a typo on line 93 of: redirection-mobile-self
One of the regex's tests .test(a) instead of .test(agent), causing a javascript error and execution to halt, therefor not redirecting.
Hi,
I've just installed this script on a desktop version website: http://www.lenotti.it/
but when I try to "force" the desktop version from a mobile device with the suggested URL
http://www.lenotti.it?noredirection=true
the script keep redirecting me to the mobile website http://mobile.lenotti.it
Could you give me any hints?
Thank you very much
Dimitri
When cookies are disabled in Chrome, session storage also disabled in such a way that it throws an exception when access is attempted. This terminates the script, and prevents redirection of mobile browsers.
Uncaught SecurityError: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document.
Great you picked this up but there is one problem,
I only need to redirect my script if the callback returns true. otherwise my callback is not able to prevent redirecting.
Stop the redirection in case some devices are passed to the function
Hi there,
the redirection works great for i.e. ios 4 but on my devices (4s, 3gs) with ios5 it doesn't.
any ideas? :-)
best
stefan
With respect to this Script:
<script> SA.redirection_mobile ({ mobile_url : "whatever.com/example", mobile_prefix : "https" }); </script>Where must the tablet redirect line be placed, thus far its inclusion depending on placement has either not worked, or in the worst case caused it to stop redirecting mobile phones as well.
Is there a way in the Redirection-Mobile.JS code to change the default to redirect tablets as well? (I am fairly new to javascript)
Thanks
Is there any way to force a mobile device that would normally be redirected to be allowed to see the normal landing page.
What i want is to be able to have a link back to standard view after a user is redirected like many websites do at the moment.
Hi,
I copy pasted the below code from example and found that tablet redirect not working.
SA.redirection_mobile ({
mobile_url : "mobile.whatever.com",
tablet_url : "tablet.whatever.com",
});
Finally I found that, you have mentioned var "tablet_host" as "tablet_url". So please correctthat typo error in the examples section. Now its working fine, thanks for the plugin.
Hello, i download the zip file and uploaded the test folder for test. I have alot of error like, Died on test #1 Cant find variable SA
Hey, wonderfully easy script. We deployed it to several pages on a plone-based cms site recently and it worked fine on a number of devices when we first tested, but for some reason we are finding that some android devices are failing to execute the script. We've had it fail on several droids and a newer samsung infuse, but it works fine when using firefox's user-agent switch and setting the UA to the various flavors of android. One tester claims that the site did redirect correctly several months ago, but that it now fails. This would make me think the problem was with the the device dealing with cookies/storage, but the samsung infuse we tested on had never visited the site before. It continues to work fine in IOS devices so I'm guessing something in the expression that test for android?
I also found this site with a big Regex test for devices, maybe useful for yours as well? http://detectmobilebrowsers.com/
Our Site: library.unt.edu.
Thanks for your hard work!
Please double check this model
Despite this behavior being disabled by default, the iPad is still being affected.
Check the redirection with www subdomain
It is in the introduction,
<script src="redireciton-mobile.js"></script>It wast me five miniuts to find out what's wrong with the code,please correct it before others wast their time.
Not a huge issue, but copying and pasting from the examples in redirection_mobile.js will not work.
The tablet_redirection: true does not work for Samsung Galaxy Tab 10". I changed android 0.5 to android and it works but I wonder if I broke something else on other devices. FYI, the UA for this device is shown in my logs as:
Mozilla/5.0 (Linux; U; Android 3.2; en-us; GT-P7510 Build/HTJ85B) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/5
so I suppose other than android, we have GT-P7510 as a potential for defining the device.
Great script!
Hi, I am calling the js on the page header, giving it a if is_front_page so that it will only get redirected from the website's homepage. However, I am trying to tackle the fact that my website is SSL secured (https://) and my mobile website is not (http://m.).
At the current code the http prefix (or https) is matched. Can you point out where this can be specified?
I am calling the code like this, in my wordpress head:
<?php wp_head(); ?>
<?php if ( is_front_page() ) { ?><script type="text/javascript" src="https://piccolo.co.il/js/redirection-mobile.js"></script><script type="text/javascript">// <![CDATA[
SA.redirection_mobile ({
mobile_url : "m.piccolo.co.il",
});
// ]]></script>';}<?php } ?>
Many thanks
Hi, I would like to allow the iPad to go to the noraml version of the site instead of being redirected to the mobile version, is there an option/code a can change to make this happen..?
I'd like to use the redirection_mobile_self.js since it's less code, but is there a way to make it stop redirecting the iPad?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.