Skip to content

Instantly share code, notes, and snippets.

@duonoid
Created April 11, 2012 18:36
Show Gist options
  • Save duonoid/2361259 to your computer and use it in GitHub Desktop.
Save duonoid/2361259 to your computer and use it in GitHub Desktop.
// example usage:
// <script type="text/javascript" src="redirector.js"></script>
//
// <script type="text/javascript">
// (function (global) {
// redir = global.Redirector;
//
// redir.width = 1400; // optional
// redir.base_url = "http://example.com";
// redir.redirect();
// }) (window);
// </script>
//
(function (global) {
'strict mode';
global.Redirector = global.Redirector || {
width: 700,
noredirect_param: 'noredirect',
stripped_url: function () {
return this.base_url.replace(/\/$/, '');
},
path: function () {
var a = document.createElement('a')
a.href = global.location;
return a.pathname + a.search + a.hash;
},
redirect_to: function () {
return this.stripped_url() + this.path();
},
noredirect: function () {
return new RegExp(this.noredirect_param, 'i').test(global.location);
},
redirect: function () {
if (this.noredirect()) return;
if (screen.width < this.width) {
global.location = this.redirect_to();
}
}
};
}) (window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment