How does this piece of JavaScript work?
<p>This code is just stuck in the Body  it does exactly what I expect but I dont understand WHY. </p>

<p>Particulaly I dont see how the webservice gets called, it looks like the script adds the call to the Head section and meybe reloads the page but im not sure and dont like not knowing or what this line means - script.onload = script.onreadystatechange = function () </p>

<p>Can anyone explain please?</p>

<pre><code>var script = document.createElement("script"),
    head = document.getElementsByTagName("head")[0],
    url = "https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/FindByPostcode/v1.00/json.ws?";

// Build the query string
url += "&amp;Key=" + encodeURI(pca_Key);
url += "&amp;Postcode=" + encodeURI(postcode);
url += "&amp;CallbackFunction=PostcodeAnywhere_FindByPostcode_End";

script.src = url;

// Make the request
script.onload = script.onreadystatechange = function () {
    if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
        script.onload = script.onreadystatechange = null;
        if (head &amp;&amp; script.parentNode)
            head.removeChild(script);
    }
}

head.insertBefore(script, head.firstChild);
</code></pre>

<p>Stu</p>
