Skip to content

Instantly share code, notes, and snippets.

Created March 22, 2013 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5219721 to your computer and use it in GitHub Desktop.
Save anonymous/5219721 to your computer and use it in GitHub Desktop.
The question came up recently about the possibility of extending an existing javascript without duplicating it. Yesterday, I found out. I think the answer is to re-wrap an existing function with modifications in an 'anonymous function.' I have a page where I check for some required browser plugins before sending users on to our eLearning site; A…
<script type="text/javascript" src="http://www.java.com/js/deployJava.js"></script>
<script type="text/javascript">
//Find browser (for non Safari)
if( ! jQuery.browser.safari ){
/* *********************************
* Modify deployJava.getBrowser()
********************************* */
/**
* Usage: browserDetect(); //execute the script
*/
browserDetectScript=""; //set defaults
//capture remote script contents, minus the surrounding function() and brackets {}
browserDetectScript = deployJava.getBrowser.toString().replace('function (){','').replace(/\}$/,'');
//console.log(browserDetectScript); //view the original script contents
//check the function contents for any key piece
if(browserDetectScript.indexOf("return this.browserName")){
//edit the function
//return a different var
browserDetectScript = browserDetectScript.replace('return this.browserName','return this.browserName2');
//remove the log comments
browserDetectScript = browserDetectScript.replace('g("[getBrowser()] navigator.userAgent.toLowerCase() -> "+n);','');
browserDetectScript = browserDetectScript.replace('g("[getBrowser()] Detected browser name:"+this.browserName+", "+this.browserName2)','');
}
//create the new function
var browserDetect = new Function(browserDetectScript);
//console.log(browserDetect.toString()); //view the modified script contents
/* *************************************
* END Modify deployJava.getBrowser()
************************************* */
} else {
function browserDetect(){
return "Safari";
}
}//end if not safari
//Browser compatibility
currentBrowser = browserDetect();
if( !currentBrowser.length > 0 ) {
currentBrowser = yourBrowser();
}
console.log(yourOS() + ' + ' + currentBrowser);
</script>
//Console output; Note the return statement at the end of each code block here:
if(this.browserName==null){var n=navigator.userAgent.toLowerCase();g("[getBrowser()] navigator.userAgent.toLowerCase() ->
"+n);if((n.indexOf("msie")!=-1)&&(n.indexOf("opera")==-1)){this.browserName="MSIE";this.browserName2="MSIE"}else{if(n.indexOf("iphone")!=-1){this.browserName="Netscape
Family";this.browserName2="iPhone"}else{if((n.indexOf("firefox")!=-1)&&(n.indexOf("opera")==-1)){this.browserName="Netscape
Family";this.browserName2="Firefox"}else{if(n.indexOf("chrome")!=-1){this.browserName="Netscape
Family";this.browserName2="Chrome"}else{if(n.indexOf("safari")!=-1){this.browserName="Netscape
Family";this.browserName2="Safari"}else{if((n.indexOf("mozilla")!=-1)&&(n.indexOf("opera")==-1)){this.browserName="Netscape
Family";this.browserName2="Other"}else{if(n.indexOf("opera")!=-1){this.browserName="Netscape
Family";this.browserName2="Opera"}else{this.browserName="?";this.browserName2="unknown"}}}}}}}g("[getBrowser()] Detected browser
name:"+this.browserName+", "+this.browserName2)}return this.browserName
function anonymous() {
if(this.browserName==null){var
n=navigator.userAgent.toLowerCase();if((n.indexOf("msie")!=-1)&&(n.indexOf("opera")==-1)){this.browserName="MSIE";this.browserName2="MSIE"
}else{if(n.indexOf("iphone")!=-1){this.browserName="Netscape
Family";this.browserName2="iPhone"}else{if((n.indexOf("firefox")!=-1)&&(n.indexOf("opera")==-1)){this.browserName="Netscape
Family";this.browserName2="Firefox"}else{if(n.indexOf("chrome")!=-1){this.browserName="Netscape
Family";this.browserName2="Chrome"}else{if(n.indexOf("safari")!=-1){this.browserName="Netscape
Family";this.browserName2="Safari"}else{if((n.indexOf("mozilla")!=-1)&&(n.indexOf("opera")==-1)){this.browserName="Netscape
Family";this.browserName2="Other"}else{if(n.indexOf("opera")!=-1){this.browserName="Netscape
Family";this.browserName2="Opera"}else{this.browserName="?";this.browserName2="unknown"}}}}}}}}return this.browserName2
}
Macintosh + Firefox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment