UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

104 lines (91 loc) 2.99 kB
--- layout: page title: "JavaScript phpversion function" comments: true sharing: true footer: true alias: - /functions/view/phpversion:603 - /functions/view/phpversion - /functions/view/603 - /functions/phpversion:603 - /functions/603 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's phpversion {% codeblock info/phpversion.js lang:js https://raw.github.com/kvz/phpjs/master/functions/info/phpversion.js raw on github %} function phpversion () { // http://kevin.vanzonneveld.net // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: We are using this to get the JavaScript version (since this is JavaScript and we can't get the PHP version anyways) // % note 2: The return value will depend on your client's JavaScript version // % note 3: Uses global: php_js to store environment info // * example 1: phpversion(); // * returns 1: '1.8' var xhtml = true, s = {}, firstScript = {}, d = this.window.document, c = 'createElement', cn = 'createElementNS', xn = 'http://www.w3.org/1999/xhtml', g = 'getElementsByTagName', gn = 'getElementsByTagNameNS'; // BEGIN REDUNDANT this.php_js = this.php_js || {}; // END REDUNDANT var getVersion = function (app) { var att = '', minVers = 0, versionString = '', temp_jsversion = undefined; if (this.php_js.jsversion !== undefined) { return this.php_js.jsversion; } while (this.php_js.jsversion === temp_jsversion && minVers < 10) { temp_jsversion = '1.' + minVers; if (gn) { firstScript = d[gn](xn, 'script')[0]; } if (!firstScript) { firstScript = d[g]('script')[0]; xhtml = false; } if (d[cn] && xhtml) { s = d[cn](xn, 'script'); } else { s = d[c]('script'); } if (app) { // Check with standard attribute (but not cross-browser standardized value?) as per Mozilla att = 'type'; versionString = 'application/javascript;version=1.'; } else { att = 'language'; // non-standard versionString = 'JavaScript1.'; } s.setAttribute(att, versionString + minVers); s.appendChild(d.createTextNode("this.php_js.jsversion=" + "'1." + minVers + "'")); firstScript.parentNode.insertBefore(s, firstScript); s.parentNode.removeChild(s); minVers++; } return this.php_js.jsversion; }; getVersion(true); getVersion(false); return this.php_js.jsversion; } {% endcodeblock %} - [view on github](https://github.com/kvz/phpjs/blob/master/functions/info/phpversion.js) - [edit on github](https://github.com/kvz/phpjs/edit/master/functions/info/phpversion.js) ### Example 1 This code {% codeblock lang:js example %} phpversion(); {% endcodeblock %} Should return {% codeblock lang:js returns %} '1.8' {% endcodeblock %} ### Other PHP functions in the info extension {% render_partial _includes/custom/info.html %}