這段代碼只是卡在Body中,它的確如我所料,但我不明白爲什麼。這段JavaScript是如何工作的?
特別是我沒有看到如何調用web服務,它看起來像腳本將呼叫添加到頭部分,也許重新加載頁面,但我不知道,不喜歡不知道或這一行意味着什麼 - script.onload = script.onreadystatechange = function()
任何人都可以解釋一下嗎?
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 += "&Key=" + encodeURI(pca_Key);
url += "&Postcode=" + encodeURI(postcode);
url += "&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 && script.parentNode)
head.removeChild(script);
}
}
head.insertBefore(script, head.firstChild);
斯圖