AJax技术

Alternate Ajax Techniques, Part 1(2)

字号+ 作者:H5之家 来源:H5之家 2015-12-06 08:32 我要评论( )

function makeRequest(sUrl, oParams) { for (sName in oParams) { if (sUrl.indexOf("?") -1) { sUrl += ""; } else { sUrl += "?"; } sUrl += encodeURIComponent(sName) + "=" + encodeURIComponent(oParams[sNa

function makeRequest(sUrl, oParams) {
  for (sName in oParams) {
    if (sUrl.indexOf("?") > -1) {
      sUrl += "&";
    } else {
      sUrl += "?";
    }
    sUrl += encodeURIComponent(sName) + "=" + encodeURIComponent(oParams[sName]);
  }

  var oScript = document.createElement("script");
  oScript.src = sUrl;
  document.body.appendChild(oScript);
}

This function expects to be passed a URL for a JavaScript file and an object containing query string arguments. The query string is constructed inside of the function by iterating over the properties of this object. Then, the familiar dynamic script loading technique is used. This function can be called as follows:

var oParams = {
  "param1": "value1",
  "param2": "value2"
};
makeRequest("/path/to/myjs.php", oParams)

Created: March 27, 2003
Revised: February 10, 2006

URL:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评