Use PHP functions in JavaScript

JavaScript include

!No description available for include. @php.js developers: Please update the function summary text file.

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
function include (filename) {
    // !No description available for include. @php.js developers: Please update the function summary text file.
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/include    // +   original by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Legaev Andrey
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Michael White (http://getsprink.com)
    // +      input by: Brett Zamir (http://brett-zamir.me)    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      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: The included file does not come available until a second script block, so typically use this in the header.
    // %        note 3: Uses global: php_js to keep track of included files    // *     example 1: include('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');
    // *     returns 1: 1
    var d = this.window.document;
    var isXML = d.documentElement.nodeName !== 'HTML' || !d.write; // Latter is for silly comprehensiveness
    var js = d.createElementNS && isXML ? d.createElementNS('http://www.w3.org/1999/xhtml', 'script') : d.createElement('script');    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', filename);
    js.setAttribute('defer', 'defer');
    d.getElementsByTagNameNS && isXML ?
        (d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0] ?            d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0].appendChild(js) :
            d.documentElement.insertBefore(js, d.documentElement.firstChild) // in case of XUL
        ):
        d.getElementsByTagName('head')[0].appendChild(js);
    // save include state for reference by include_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;
    } else {
        this.php_js.includes[filename]++;
    }
     return this.php_js.includes[filename];
}
external links: original PHP docs | raw js source

Examples

Running

1
include('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');

Should return

1
1

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have include goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
16 Mar '08 Permalink

q  @ mark forster: The site's explanation isn't too elaborate, but I think I understand the idea. It's just like Java imports, right? I can imagine it being useful for many purposes, but I don't see how we would implement it in this project for two reasons:
- it requires jQuery (an additional 15kB) for only 1 functionality that
- resembles Java, not php

Gravatar
mark forster
16 Mar '08 Permalink

q  Why not check out owl import at http://code.google.com/p/owlimport/. an alternative method of importing javascript files

Gravatar
Kevin van Zonneveld
8 Jan '08 Permalink

q  @ Legaev Andrey: Wow great contributions again!

At first I wanted every function to be standalone, but thinking about all the redundant code we would then get, I finally decided to add a: "- depends on: include" comment to include_once. I think we should stick to that approach.

Thanks!

Gravatar
Legaev Andrey
8 Jan '08 Permalink

q  include_once() function:

[CODE="Javascript"]
function include_once( filename ) {
if (!window.php_js) window.php_js = {};
if (!window.php_js.includes) window.php_js.includes = {};

if (!window.php_js.includes[filename]) {
window.php_js.includes[filename] = 1;
include_once( filename );
}
}
[/CODE]

And small modification to include():
[CODE="Javascript"]
function include( filename ) {
// http://kevin.vanzonneveld.net
// + original by: mdsjack (http://www.mdsjack.bo.it)

var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', filename);
js.setAttribute('defer', 'defer');
document.getElementsByTagName('HEAD')[0].appendChild(js);

if (!window.php_js) window.php_js = {};
if (!window.php_js.includes) window.php_js.includes = {};

if (!window.php_js.includes[filename])
window.php_js.includes[filename] = 1;
else
window.php_js.includes[filename]++;
}
[/CODE]


Contribute a New function

More functions

In this category

» include
include_once
require
require_once

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !