Created
August 21, 2013 22:56
-
-
Save andershaig/6301272 to your computer and use it in GitHub Desktop.
Redirect Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% plugin rawtext page_url %} | |
<script type="text/javascript"> | |
if (!document.body.className.match('page_preview')) { | |
// Redirects as soon as possible | |
setInterval( function () { | |
top.location.replace({{ page_url | json }}); | |
},250); | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% plugin rawtext mobile_url %} | |
{% plugin rawtext page_url %} | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
// DETECT IF ON MOBILE DEVICE. IF YES... | |
if( navigator.userAgent.match(/Android/i) | |
|| navigator.userAgent.match(/webOS/i) | |
|| navigator.userAgent.match(/iPhone/i) | |
|| navigator.userAgent.match(/iPad/i) | |
|| navigator.userAgent.match(/iPod/i) | |
|| navigator.userAgent.match(/BlackBerry/i) | |
){ | |
top.location.href="{{ mobile_url }}" | |
} else { | |
top.location.href="{{ page_url }}"; | |
}; | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the difference?
The newer snippet checks for the
page_preview
body class to make sure it doesn't run if it's in Page Manager. It also doesn't use jQuery so it happens a bit faster and ideally you can redirect before seeing a flash of the canvas page. Lastly, it uses top.location.replace which is pretty much equivalent to top.location.href but prevents the url from being added to browser history and therefore won't mess up your back button.Why is this equivalent?
The plugin
mobile_url
goes away but that's ok because in this configuration it was never used anyways.fbcanvas.liquid
only loads on a desktop browser (otherwisemobile.liquid
loads) so it has never run on a mobile device). This switch happens before any of the attached code is looked at so it doesn't run.