Skip to content

Instantly share code, notes, and snippets.

@michaeldozark
Last active August 29, 2015 14:15
Show Gist options
  • Save michaeldozark/17412e3eeda6b391ae8c to your computer and use it in GitHub Desktop.
Save michaeldozark/17412e3eeda6b391ae8c to your computer and use it in GitHub Desktop.
Javascript conversion of WordPress vars.php needed for when I am using page caching, but still need browser-based body classes, etc.
/**
* Slimline WordPress Globals
*
* Javascript conversion of WordPress vars.php needed for when I am using page caching, but still need browser-based body classes, etc.
*
* @link https://gist.github.com/michaeldozark/17412e3eeda6b391ae8c
* @version 0.1.0
*/
/**
* slimline_set_browser_globals function
*
* Javascript-based version of WordPress browser global variables for use when we are page caching. Basically this apes
* the WordPress functions for setting browser-based global variables, with an extra variable set for Windows phones.
*
* Note that some of these are almost certainly unnecessary (e.g., Mac IE, Netscape Navigator), but are included because
* they are part of the equivalent WordPress functions and developers are used to having them available.
*
* @global bool is_chrome
* @global bool is_gecko
* @global bool is_IE
* @global bool is_IEMobile
* @global bool is_iphone
* @global bool is_lynx
* @global bool is_macIE
* @global bool is_NS4
* @global bool is_opera
* @global bool is_safari
* @global bool is_winIE
* @see https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/vars.php#L50
*/
function slimline_set_browser_globals() {
/**
* use window.* syntax to create/attach globals from inside the function
*
* @see http://snook.ca/archives/javascript/global_variable
*/
window.is_lynx = window.is_gecko = window.is_winIE = window.is_macIE = window.is_opera = window.is_NS4 = window.is_safari = window.is_chrome = window.is_iphone = window.is_IEMobile = false;
if ( typeof navigator.userAgent != 'undefined' ) {
/**
* Lynx
*/
if ( navigator.userAgent.match(/Lynx/i) ) {
is_lynx = true;
/**
* Chrome
*/
} else if ( navigator.userAgent.match(/Chrome/i) ) { // if ( navigator.userAgent.match(/Lynx/i) )
/**
* Make sure this is actually Chrome and not just IE using ChromeFrame
*/
if ( navigator.userAgent.match(/ChromeFrame/i) ) {
is_winIE = true;
} else { // if ( navigator.userAgent.match(/ChromeFrame/i) )
is_chrome = true;
} // if ( navigator.userAgent.match(/ChromeFrame/i) )
/**
* Safari
*/
} else if ( navigator.userAgent.match(/Safari/i) ) { // if ( navigator.userAgent.match(/Lynx/i) )
is_safari = true;
/**
* IE
*/
} else if ( navigator.userAgent.match(/MSIE/i) || navigator.userAgent.match(/Trident/i) ) { // if ( navigator.userAgent.match(/Lynx/i) )
/**
* Mac IE or Windows?
*/
if ( navigator.userAgent.match(/Mac/i) ) {
is_macIE = true;
} else { // if ( navigator.userAgent.match(/Mac/i) )
is_winIE = true;
/**
* desktop or mobile?
*/
if ( slimline_wp_is_mobile() ) {
is_IEMobile = true;
} // if ( slimline_wp_is_mobile() )
} // if ( navigator.userAgent.match(/Mac/i) )
/**
* Firefox and other Gecko-based browsers
*
* We place this after Chrome, IE and Safari since they all list "like Gecko" in their UA strings
*/
} else if ( navigator.userAgent.match(/Gecko/i) ) { // if ( navigator.userAgent.match(/Lynx/i) )
is_gecko = true;
/**
* Opera
*/
} else if ( navigator.userAgent.match(/Opera/i) ) { // if ( navigator.userAgent.match(/Lynx/i) )
is_opera = true;
/**
* Netscape 4
*/
} else if ( navigator.userAgent.match(/Nav/i) && navigator.userAgent.match(/Mozilla\/4./i) ) { // if ( navigator.userAgent.match(/Lynx/i) )
is_NS4 = true;
}
is_iphone = ( is_safari && slimline_wp_is_mobile() );
is_IE = ( is_winIE || is_macIE );
} // if ( typeof navigator.userAgent != 'undefined' )
}
/**
* slimline_wp_is_mobile function
*
* Javascript equivalent for wp_is_mobile() useful for when page caching is enabled.
*
* @global is_mobile
* @see https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/vars.php#L118
*/
function slimline_wp_is_mobile() {
/**
* check is mobile has already been set. If not, set it.
*/
if ( typeof window.is_mobile == 'undefined' ) {
/**
* use window.* syntax to create/attach globals from inside the function
*
* @see http://snook.ca/archives/javascript/global_variable
*/
window.is_mobile = false;
if ( navigator.userAgent.match(/Mobile/i)
|| navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/IEMobile/i)
|| navigator.userAgent.match(/Kindle/i)
|| navigator.userAgent.match(/Opera Mini/i)
|| navigator.userAgent.match(/Opera Mobi/i)
|| navigator.userAgent.match(/Silk/i) ) {
is_mobile = true;
}
} // if ( typeof window.is_mobile == 'undefined' )
return is_mobile;
}
/**
* automatically set the globals on document ready.
*/
jQuery(function($){
slimline_set_browser_globals();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment