Advertisement
Guest User

Untitled

a guest
May 30th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.  
  3.     $( document ).ready( function() {
  4.        
  5.         var rules = {
  6.             desktop: { min: 1024 } ,
  7.             tablet: { min: 900 , max: 1023 , prefix: "/tablet" } ,
  8.             phone: { min: 0 , max: 899 , prefix: "/phone" }
  9.         };
  10.  
  11.         function init( rules , params ) {
  12.             var _this = this;
  13.  
  14.             this.rules = rules;
  15.             this.params = params;
  16.             if ( !this.params ) this.params = {};
  17.             if (this.params.hide_history===undefined) this.params.hide_history = true;
  18.             if (this.params.real_width===undefined) this.params.real_width = true;
  19.             if (this.params.hook_resize===undefined) this.params.hook_resize = true;
  20.             if (this.params.iframe_load===undefined) this.params.iframe_load = true;
  21.             if (this.params.iframe_no_load_this===undefined) this.params.iframe_no_load_this = true;
  22.        
  23.             this.prefixes = [];
  24.             for( var i in this.rules )
  25.                 if ( this.rules[i].prefix !== undefined )
  26.                     this.prefixes.push( this.rules[i].prefix.toLowerCase() );          
  27.        
  28.             this.curr_prefix = { length: 0 , prefix: "" , pathname_last: window.location.pathname };
  29.             this.set_curr_prefixes_url = function() {
  30.                 var pathname = window.location.pathname.toLowerCase();
  31.                
  32.                 this.curr_prefix = { length: 0 , prefix: "" , pathname_last: window.location.pathname };
  33.                
  34.                 for(var i in this.prefixes) {
  35.                     if ( pathname.indexOf( this.prefixes[i] ) === 0 ) {
  36.                         var c = pathname[ this.prefixes[i].length ];
  37.                         if ( !( ( !c ) || ( c == "/" ) || ( c == "?" ) || ( c == "#" ) ) )
  38.                             continue;
  39.                         this.curr_prefix.length = this.prefixes[i].length;
  40.                         this.curr_prefix.prefix = this.prefixes[i];
  41.                         this.curr_prefix.pathname_last = window.location.pathname.substr( this.prefixes[i].length );
  42.                         return true;
  43.                     }
  44.                 }
  45.             }
  46.        
  47.             this.set_curr_prefixes_url();
  48.            
  49.             this.curr_iframe = function() {
  50.                 var isFramed = false;
  51.                 try {
  52.                     isFramed = window != window.top || document != top.document || self.location != top.location;
  53.                 } catch (e) {
  54.                     isFramed = true;
  55.                 }
  56.                 return isFramed;
  57.             }
  58.            
  59.             this.iframe_load = function( end , no_this ) {
  60.                 if ( !end || this.curr_iframe() )
  61.                     return;
  62.    
  63.                 var css = {
  64.                     "position": "fixed" ,
  65.                     "top": "-20px" ,
  66.                     "width": "4px" ,
  67.                     "height": "4px"
  68.                 };
  69.                 var prf = window.location.protocol + '//' + window.location.hostname;
  70.                 for( var i in this.prefixes ) {
  71.                     if ( this.curr_prefix.prefix && no_this && this.curr_prefix.prefix.toLowerCase() == this.prefixes[i].toLowerCase() )
  72.                         continue;
  73.                     $( "<iframe />" ).css( css ).attr( "src" , prf + this.prefixes[i] + this.curr_prefix.pathname_last ).appendTo( "body" );
  74.                 }
  75.                 if ( this.curr_prefix.prefix && no_this )
  76.                     $( "<iframe />" ).css( css ).attr( "src" , prf + this.curr_prefix.pathname_last ).appendTo( "body" );
  77.             }
  78.             this.iframe_load( this.params.iframe_load , this.params.iframe_no_load_this );
  79.            
  80.             this.redirect = function( nw , rp ) {
  81.                 if ( rp ) window.location.replace( nw );
  82.                 else window.location.assign( nw );
  83.                 //else window.location.href = nw;
  84.             }
  85.        
  86.             this.prepare_href = function( prefix , protocol ) {
  87.                 if ( !prefix )
  88.                     prefix = "";
  89.                 if ( !protocol )
  90.                     protocol = window.location.protocol;
  91.                 var hostname = window.location.hostname;
  92.                 var last = window.location.href.substr( window.location.href.indexOf(hostname) + hostname.length + this.curr_prefix.length );
  93.                 return protocol + '//' + hostname + prefix + last;
  94.             }
  95.  
  96.             this.get_width = function( real_width ) {
  97.                 if ( !real_width )
  98.                     return $( document ).width();
  99.                 return $( window ).width();
  100.             }
  101.            
  102.             this.process_redirect = false;
  103.            
  104.             this.try_width = function() {
  105.                 if ( this.process_redirect )
  106.                     return true;
  107.  
  108.                 var w = this.get_width( this.params.real_width );
  109.  
  110.                 for( var i in this.rules ) {
  111.                     var rule = this.rules[i];
  112.                
  113.                     if ( ( (rule.min===undefined) || (w >= rule.min) ) &&
  114.                         ( (rule.max===undefined) || (w <= rule.max) ) ) {
  115.                        
  116.                         var rule_pefix = rule.prefix ? rule.prefix.toLowerCase() : false;
  117.                        
  118.                         if ( ( this.curr_prefix.prefix == rule_pefix ) ||
  119.                                 ( !this.curr_prefix.prefix && !rule.prefix ) )
  120.                             continue;
  121.  
  122.                         this.redirect(
  123.                             this.prepare_href( rule.prefix ) ,
  124.                             this.params.hide_history
  125.                         );
  126.                         this.process_redirect = true;
  127.                         return true;
  128.                     }
  129.                 }
  130.                 return false;
  131.             }
  132.    
  133.             if ( this.params.hook_resize ) {
  134.                 $( window ).on( 'resize.psf_f23dzbx' , function() {
  135.                     _this.try_width();
  136.                 } );
  137.             }
  138.    
  139.             this.try_width();
  140.    
  141.         }
  142.        
  143.         var exw = new init( rules , {
  144.             hide_history: false ,
  145.             real_width: true ,
  146.             time_interval: 1 ,
  147.             hook_resize: true ,
  148.             iframe_load: true ,
  149.             iframe_no_load_this: true
  150.         } );
  151.        
  152.     } );
  153. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement