Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($) {
- $( document ).ready( function() {
- var rules = {
- desktop: { min: 1024 } ,
- tablet: { min: 900 , max: 1023 , prefix: "/tablet" } ,
- phone: { min: 0 , max: 899 , prefix: "/phone" }
- };
- function init( rules , params ) {
- var _this = this;
- this.rules = rules;
- this.params = params;
- if ( !this.params ) this.params = {};
- if (this.params.hide_history===undefined) this.params.hide_history = true;
- if (this.params.real_width===undefined) this.params.real_width = true;
- if (this.params.hook_resize===undefined) this.params.hook_resize = true;
- if (this.params.iframe_load===undefined) this.params.iframe_load = true;
- if (this.params.iframe_no_load_this===undefined) this.params.iframe_no_load_this = true;
- this.prefixes = [];
- for( var i in this.rules )
- if ( this.rules[i].prefix !== undefined )
- this.prefixes.push( this.rules[i].prefix.toLowerCase() );
- this.curr_prefix = { length: 0 , prefix: "" , pathname_last: window.location.pathname };
- this.set_curr_prefixes_url = function() {
- var pathname = window.location.pathname.toLowerCase();
- this.curr_prefix = { length: 0 , prefix: "" , pathname_last: window.location.pathname };
- for(var i in this.prefixes) {
- if ( pathname.indexOf( this.prefixes[i] ) === 0 ) {
- var c = pathname[ this.prefixes[i].length ];
- if ( !( ( !c ) || ( c == "/" ) || ( c == "?" ) || ( c == "#" ) ) )
- continue;
- this.curr_prefix.length = this.prefixes[i].length;
- this.curr_prefix.prefix = this.prefixes[i];
- this.curr_prefix.pathname_last = window.location.pathname.substr( this.prefixes[i].length );
- return true;
- }
- }
- }
- this.set_curr_prefixes_url();
- this.curr_iframe = function() {
- var isFramed = false;
- try {
- isFramed = window != window.top || document != top.document || self.location != top.location;
- } catch (e) {
- isFramed = true;
- }
- return isFramed;
- }
- this.iframe_load = function( end , no_this ) {
- if ( !end || this.curr_iframe() )
- return;
- var css = {
- "position": "fixed" ,
- "top": "-20px" ,
- "width": "4px" ,
- "height": "4px"
- };
- var prf = window.location.protocol + '//' + window.location.hostname;
- for( var i in this.prefixes ) {
- if ( this.curr_prefix.prefix && no_this && this.curr_prefix.prefix.toLowerCase() == this.prefixes[i].toLowerCase() )
- continue;
- $( "<iframe />" ).css( css ).attr( "src" , prf + this.prefixes[i] + this.curr_prefix.pathname_last ).appendTo( "body" );
- }
- if ( this.curr_prefix.prefix && no_this )
- $( "<iframe />" ).css( css ).attr( "src" , prf + this.curr_prefix.pathname_last ).appendTo( "body" );
- }
- this.iframe_load( this.params.iframe_load , this.params.iframe_no_load_this );
- this.redirect = function( nw , rp ) {
- if ( rp ) window.location.replace( nw );
- else window.location.assign( nw );
- //else window.location.href = nw;
- }
- this.prepare_href = function( prefix , protocol ) {
- if ( !prefix )
- prefix = "";
- if ( !protocol )
- protocol = window.location.protocol;
- var hostname = window.location.hostname;
- var last = window.location.href.substr( window.location.href.indexOf(hostname) + hostname.length + this.curr_prefix.length );
- return protocol + '//' + hostname + prefix + last;
- }
- this.get_width = function( real_width ) {
- if ( !real_width )
- return $( document ).width();
- return $( window ).width();
- }
- this.process_redirect = false;
- this.try_width = function() {
- if ( this.process_redirect )
- return true;
- var w = this.get_width( this.params.real_width );
- for( var i in this.rules ) {
- var rule = this.rules[i];
- if ( ( (rule.min===undefined) || (w >= rule.min) ) &&
- ( (rule.max===undefined) || (w <= rule.max) ) ) {
- var rule_pefix = rule.prefix ? rule.prefix.toLowerCase() : false;
- if ( ( this.curr_prefix.prefix == rule_pefix ) ||
- ( !this.curr_prefix.prefix && !rule.prefix ) )
- continue;
- this.redirect(
- this.prepare_href( rule.prefix ) ,
- this.params.hide_history
- );
- this.process_redirect = true;
- return true;
- }
- }
- return false;
- }
- if ( this.params.hook_resize ) {
- $( window ).on( 'resize.psf_f23dzbx' , function() {
- _this.try_width();
- } );
- }
- this.try_width();
- }
- var exw = new init( rules , {
- hide_history: false ,
- real_width: true ,
- time_interval: 1 ,
- hook_resize: true ,
- iframe_load: true ,
- iframe_no_load_this: true
- } );
- } );
- })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement