Forum Moderators: open
<script type="text/javascript">
function loadScript(url, callback) {
var js = document.createElement('script');
js.type = 'text/javascript';
js.onload = callback;
//IE won't respond to onload with script elements, uses onreadystatechange. No harm assigning in non-IE
js.onreadystatechange = function () {
var rs = js.readyState;
//alert(rs); //UNCOMMENT THIS ALERT IF YOU WISH TO SEE THE readyState
//js.readyState when script is fully loaded will be 'loaded' if it was not already
//in the browser's cache, or 'complete' if it was already cached.
//Is 'loading' while in process of loading
if (typeof callback == 'function' && rs.match(/(loaded|complete)/i)) {
callback();
}
};
//best not to define the script element's src until
//after onload and onreadystatechange have been defined
js.src = url;
document.body.appendChild(js);
}
window.onload = function () {
var url = '../js_scripts/general_utilities.js',
callback = function () {
//my 'general_utilities.js' file has a function
//named getById, so let's get notified when it's available:
alert('Loaded script file: '+ url +'\ntype of getById = '+ (typeof getById)); //type of getById = function! Good, now we may use it!
};
loadScript(url, callback);
};
</script>
<?php
//>loadScript.php
define('ROOT', preg_replace('/\/$/', '', $_SERVER['DOCUMENT_ROOT']));
define('SITE_NAME', preg_replace("/^www\./", '', $_SERVER['SERVER_NAME']));
define('JS_FOLDER', ROOT.'/js_scripts'); //SET JS_FOLDER TO THE LOCATION OF YOUR FOLDER CONTAINING ALL JAVASCRIPTS
//Note that only this script 'knows' where your .js files are stored now, if
//you use only this file to load your scripts. Of course the scripts are still easily accessible in browsers
$scriptName = $_GET['scriptName'];
//the scriptName can be passed with or without the '.js' extension at end of the file name
$fullScriptName = JS_FOLDER.'/'.preg_replace("/\.js$/", '', $scriptName).'.js';
$callback = $_GET['callback'];
$referrer = $_SERVER['HTTP_REFERER'];
if (!preg_match('/https?:\/\/(www.)?'.SITE_NAME.'/', $referrer)) {
//haha, anyone trying to direct download the script via
//the url to loadScript.php in the page's source will
//just get a script with an alert of "Hello World"
$scriptContent = 'alert("Hello World!");';
} else if (is_string($scriptName) && strlen($scriptName) && file_exists($fullScriptName)) {
$scriptContent = file_get_contents($fullScriptName);
if (is_string($callback) && strlen($callback)) {
$scriptContent .= "\r\n\r\n".$callback; //assuming $callback is defined as a function call with parens () to invoke it
if (!preg_match("/\(.*\)/", $callback)) { //the $callback is not defined as a function call with parens already, is just name of function to invoke
$scriptContent .= '();';
}
}
//REMOVE OR COMMENT OUT THE BELOW LINE FOR FINAL PRODUCTION USAGE, IS JUST HERE FOR TESTING/EXAMPLE PURPOSES:
$scriptContent .= "\r\n\r\nalert('this alert directly from end of file: $scriptName, referrer: $referrer');";
} else {
$scriptContent = '//File not found';
}
header('Content-type: text/javascript');
echo $scriptContent;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Some Title</title>
<!-- loading a script with no 'callback' required upon it's loading: -->
<script type="text/javascript" src="http://www.example.com/loadScript.php?scriptName=justSomeStuff"></script>
<script type="text/javascript">
function loadScript(url) {
var js = document.createElement('script');
js.src = url;
js.type = 'text/javascript';
document.body.appendChild(js);
}
function registerScriptLoad(message) {
alert(message);
}
</script>
<script type="text/javascript"
src="http://www.example.com/loadScript.php?scriptName=generalUtilities&callback=registerScriptLoad('Loaded%20script%20file:%20generalUtilities.js');"></script>
<!-- note loading the below script, it's callback has no parens (), but the php file will add them (when they are not present) to invoke the BogusObject.sayHi method -->
<script type="text/javascript" src="http://www.example.com/loadScript.php?scriptName=BogusObject&callback=BogusObject.sayHi"></script>
</head>
<body>
<div>
<button type="button"
onclick="loadScript('http://www.example.com/loadScript.php?scriptName=logger&callback=LOGGER.log(\'Loaded script file: logger.js\')');">Load Script</button>
</div>
</body>
</html>
function chat() {ondemand('chat','chat_init1');}