文章目录:
此种方法与Jsonp方法类似。是呼叫js文件。通过文件传值。方法如下。
1、在B域(B.com)中建立一个Js文件 JsCreateJson.js。此js返回一个json。代码如下。
{ symbol: 'IBM', price: 91.42 }
2、在A域(A.com)使用CrossByReferenceJs.aspx动态调用此文件。代码如下。
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeBehind
=
"
CrossByReferenceJs.aspx.cs
"
Inherits = " IframeTest.CrossByReferenceJs " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22 >
< html xmlns ="http://www.w3.org/1999/xhtml%22>
<head runat=" server" >
< title ></ title >
< script type ="text/javascript" >
function showPrice(data) {
alert( " Symbol: " + data.symbol + " , Price: " + data.price);
}
var JSONP = document.createElement( " script " );
// FF:onload IE:onreadystatechange
JSONP.onload = JSONP.onreadystatechange = function () {
// onreadystatechange,仅IE
if ( ! this .readyState || this .readyState === " loaded " || this .readyState === " complete " ) {
JSONP.onload = JSONP.onreadystatechange = null // 请内存,防止IE memory leaks
}
}
JSONP.type = " text/javascript " ;
JSONP.src = " http://b.com/JsCreateJson.js%22";
//在head之后添加js文件
document.getElementsByTagName( " head " )[0].appendChild(JSONP);
</script>
</head>
<body>
<form id= " form1 " runat= " server " >
<div id= " demo " >
<%--显示调用结果--%>
<input id= " Button1 " type= " button " value= " button " onclick= " showPrice() " />
</div>
</form>
</body>
</html>
Inherits = " IframeTest.CrossByReferenceJs " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22 >
< html xmlns ="http://www.w3.org/1999/xhtml%22>
<head runat=" server" >
< title ></ title >
< script type ="text/javascript" >
function showPrice(data) {
alert( " Symbol: " + data.symbol + " , Price: " + data.price);
}
var JSONP = document.createElement( " script " );
// FF:onload IE:onreadystatechange
JSONP.onload = JSONP.onreadystatechange = function () {
// onreadystatechange,仅IE
if ( ! this .readyState || this .readyState === " loaded " || this .readyState === " complete " ) {
JSONP.onload = JSONP.onreadystatechange = null // 请内存,防止IE memory leaks
}
}
JSONP.type = " text/javascript " ;
JSONP.src = " http://b.com/JsCreateJson.js%22";
//在head之后添加js文件
document.getElementsByTagName( " head " )[0].appendChild(JSONP);
</script>
</head>
<body>
<form id= " form1 " runat= " server " >
<div id= " demo " >
<%--显示调用结果--%>
<input id= " Button1 " type= " button " value= " button " onclick= " showPrice() " />
</div>
</form>
</body>
</html>