Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to use AJAX to retrieve the PHP output; get the clients to add a <code>&lt;script /&gt;</code> tag to their pages with the <code>src</code> attribute set to your PHP output; just make sure your script emits valid JavaScript.</p> <p>As your web server now thinks it's in PHP land rather than JS assets, be sure to emit correct page headers; MIME type and expires spring to mind.</p> <pre><code>header('Content-Type: application/x-javascript'); header('Last-Modified: Mon, 03 Sep 2012 22:33:44 GMT'); </code></pre> <p>Furthermore, be aware of the un-reliable, and "hostile" environment your script will be executing in. You should minimize the amount of global variables your JavaScript script declares to mitigate the risks of collisions. The easiest way to do this is to wrap your code in an IIFE;</p> <pre><code>// Look ma, no global variables! (function () { var thebody = document.getElementsByTagName('body')[0]; var newdiv = document.createElement('div'); var txtNode = document.createTextNode("Hello. This is a new node."); var divIdName = "mydiv"; var csslink = "http://www.anotherwebsite.com/style.css"; var newcss = document.createElement('link'); newcss.setAttribute('href',csslink); newcss.setAttribute('rel','stylesheet'); newcss.setAttribute('type','text/css'); newdiv.setAttribute('id',divIdName); newdiv.appendChild(txtNode); thebody.appendChild(newdiv); thebody.appendChild(newcss); }()); </code></pre> <p>You should also consider prefixing your CSS classes, element ID's etc to minimize collisions with the hosts naming schemes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload