Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49567
  • 博文数量: 19
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-08 11:40
文章分类

全部博文(19)

文章存档

2011年(1)

2009年(18)

我的朋友
最近访客

分类:

2009-08-26 16:09:11

一片重要的文章,《JSON and the Dynamic Script Tag: Easy, XML-less Web Services for JavaScript》
地址:



附录:

(注:webservice要生成合适的javascript代码返回,并调用回调函数)


// A SECURITY WARNING FROM DOUGLAS CROCKFORD:

// "The dynamic

//

// function callbackfunc(jsonData) {

// alert('Latitude = ' + jsonData.ResultSet.Result[0].Latitude +

// ' Longitude = ' + jsonData.ResultSet.Result[0].Longitude);

// aObj.removeScriptTag();

// }

//

// request = '

// output=json&callback=callbackfunc&location=78704';

// aObj = new JSONscriptRequest(request);

// aObj.buildScriptTag();

// aObj.addScriptTag();

//

//



// Constructor -- pass a REST request URL to the constructor

//

function JSONscriptRequest(fullUrl) {
    // REST request path

    this.fullUrl = fullUrl;
    // Keep IE from caching requests

    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag

    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id

    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter

JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method

//

JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag

    this.scriptObj = document.createElement("script");
    
    // Add script object attributes

    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method

//

JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag

    this.headLoc.removeChild(this.scriptObj);
}

// addScriptTag method

//

JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag

    this.headLoc.appendChild(this.scriptObj);
}


另一个可能的有用的文章:《Bidynodes: two-way, cross-domain communication in the
阅读(974) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~