Skip to content

Instantly share code, notes, and snippets.

@andershaig
Created August 21, 2013 22:56
Show Gist options
  • Save andershaig/6301272 to your computer and use it in GitHub Desktop.
Save andershaig/6301272 to your computer and use it in GitHub Desktop.
Redirect Fix
{% 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>
{% 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>
@andershaig
Copy link
Author

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 (otherwise mobile.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment