phpjs
Version:
86 lines (75 loc) • 3.29 kB
Markdown
---
layout: page
title: "JavaScript require function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/require:502
- /functions/view/require
- /functions/view/502
- /functions/require:502
- /functions/502
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's require
{% codeblock language/require.js lang:js https://raw.github.com/kvz/phpjs/master/functions/language/require.js raw on github %}
function require (filename) {
// http://kevin.vanzonneveld.net
// + original by: Michael White (http://getsprink.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Yen-Wei Liu
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// % note 1: Force Javascript execution to pause until the file is loaded. Usually causes failure if the file never loads. ( Use sparingly! )
// % note 2: Uses global: php_js to keep track of included files
// - depends on: file_get_contents
// * example 1: require('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');
// * returns 1: 2
var d = this.window.document;
var isXML = d.documentElement.nodeName !== 'HTML' || !d.write; // Latter is for silly comprehensiveness
var js_code = this.file_get_contents(filename);
var script_block = d.createElementNS && isXML ? d.createElementNS('http://www.w3.org/1999/xhtml', 'script') : d.createElement('script');
script_block.type = 'text/javascript';
var client_pc = navigator.userAgent.toLowerCase();
if ((client_pc.indexOf('msie') !== -1) && (client_pc.indexOf('opera') === -1)) {
script_block.text = js_code;
} else {
script_block.appendChild(d.createTextNode(js_code));
}
if (typeof script_block !== 'undefined') {
d.getElementsByTagNameNS && isXML ? (d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0] ? d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0].appendChild(script_block) : d.documentElement.insertBefore(script_block, d.documentElement.firstChild) // in case of XUL
) : d.getElementsByTagName('head')[0].appendChild(script_block);
// save include state for reference by include_once and require_once()
var cur_file = {};
cur_file[this.window.location.href] = 1;
// BEGIN REDUNDANT
this.php_js = this.php_js || {};
// END REDUNDANT
if (!this.php_js.includes) {
this.php_js.includes = cur_file;
}
if (!this.php_js.includes[filename]) {
this.php_js.includes[filename] = 1;
return 1;
} else {
return ++this.php_js.includes[filename];
}
}
return 0;
}
{% endcodeblock %}
- [view on github](https://github.com/kvz/phpjs/blob/master/functions/language/require.js)
- [edit on github](https://github.com/kvz/phpjs/edit/master/functions/language/require.js)
### Example 1
This code
{% codeblock lang:js example %}
require('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
2
{% endcodeblock %}
### Other PHP functions in the language extension
{% render_partial _includes/custom/language.html %}