URL | 说明 | 是否允许通信 |
---|---|---|
http://www.a.com/a.js http://www.a.com/b.js | 同一域名下 | 允许 |
http://www.a.com/lab/a.js http://www.a.com/script/b.js | 同一域名下不同文件夹 | 允许 |
http://www.a.com:8000/a.js http://www.a.com/b.js | 同一域名,不同端口 | 不允许 |
http://www.a.com/a.js https://www.a.com/b.js | 同一域名,不同协议 | 不允许 |
http://www.a.com/a.js http://70.32.92.74/b.js | 域名和域名对应ip | 不允许 |
http://www.a.com/a.js http://script.a.com/b.js | 主域相同,子域不同 | 不允许 |
http://www.a.com/a.js http://a.com/b.js | 同一域名,不同二级域名(同上) | 不允许(cookie这种情况下也不允许访问) |
http://www.cnblogs.com/a.js http://www.a.com/b.js | 不同域名 | 不允许 |
document.domain = "a.com";var ifr = document.createElement("iframe");ifr.src = "http://www.script.a.com/b.html";ifr.display = none;document.body.appendChild(ifr);ifr.onload = function(){var doc = ifr.contentDocument || ifr.contentWindow.document;//在这里操作doc,也就是b.htmlifr.onload = null;};2) 在www.script.a.com/b.html中:
document.domain = "a.com";2> 动态创建script
function loadScript(url, func) { var head = document.head || document.getElementByTagName("head")[0]; var script = document.createElement("script"); script.src = url; script.onload = script.onreadystatechange = function(){if(!this.readyState || this.readyState=="loaded" || this.readyState=="complete"){ func(); script.onload = script.onreadystatechange = null;} }; head.insertBefore(script, 0);}window.baidu = { sug: function(data){console.log(data); }}loadScript("http://suggestion.baidu.com/su?wd=w",function(){console.log("loaded")});//我们请求的内容在哪里?//我们可以在chorme调试面板的source中看到script引入的内容3> location.hash + iframe
function startRequest(){var ifr = document.createElement("iframe");ifr.style.display = "none";ifr.src = "http://www.cnblogs.com/lab/cscript/cs2.html#paramdo";document.body.appendChild(ifr);}function checkHash() {try {var data = location.hash ? location.hash.substring(1) : "";if (console.log) {console.log("Now the data is "+data);}} catch(e) {};}setInterval(checkHash, 2000);cnblogs.com域名下的cs2.html://模拟一个简单的参数处理操作switch(location.hash){case "#paramdo":callBack();break;case "#paramset"://do something……break;}function callBack(){try {parent.location.hash = "somedata";} catch (e) {// ie、chrome的安全机制无法修改parent.location.hash,// 所以要利用一个中间的cnblogs域下的代理iframevar ifrproxy = document.createElement("iframe");ifrproxy.style.display = "none";ifrproxy.src = "http://a.com/test/cscript/cs3.html#somedata";// 注意该文件在"a.com"域下document.body.appendChild(ifrproxy);}}a.com下的域名cs3.html
//因为parent.parent和自身属于同一个域,所以可以改变其location.hash的值parent.parent.location.hash = self.location.hash.substring(1);4> window.name + iframe
<head> <script> function proxy(url, func){var isFirst = true,ifr = document.createElement("iframe"),loadFunc = function(){ if(isFirst){ifr.contentWindow.location = "http://a.com/cs1.html";isFirst = false; }else{func(ifr.contentWindow.name);ifr.contentWindow.close();document.body.removeChild(ifr);ifr.src = "";ifr = null; }};ifr.src = url;ifr.style.display = "none";if(ifr.attachEvent) ifr.attachEvent("onload", loadFunc);else ifr.onload = loadFunc;document.body.appendChild(iframe); }</script></head><body> <script>proxy("http://www.baidu.com/", function(data){ console.log(data);}); </script></body>3 在b.com/cs1.html中包含:
<script>window.name = "要传送的内容";</script>5> postMessage(HTML5中的XMLHttpRequest Level 2中的API)
<iframe id="ifr" src="b.com/index.html"></iframe><script type="text/javascript">window.onload = function() {var ifr = document.getElementById("ifr");var targetOrigin = "http://b.com"; // 若写成"http://b.com/c/proxy.html"效果一样// 若写成"http://c.com"就不会执行postMessage了ifr.contentWindow.postMessage("I was there!", targetOrigin);};</script>2) b.com/index.html中的代码:
<script type="text/javascript">window.addEventListener("message", function(event){// 通过origin属性判断消息来源地址if (event.origin == "http://a.com") {alert(event.data);// 弹出"I was there!"alert(event.source); // 对a.com、index.html中window对象的引用 // 但由于同源策略,这里event.source不可以访问window对象}}, false);</script>6> CORS
var xdr = new XDomainRequest();xdr.onload = function(){console.log(xdr.responseText);}xdr.open("get", "http://www.baidu.com");......xdr.send(null);其它浏览器中的实现就在xhr中
var xhr = new XMLHttpRequest();xhr.onreadystatechange = function () {if(xhr.readyState == 4){if(xhr.status >= 200 && xhr.status < 304 || xhr.status == 304){console.log(xhr.responseText);}}}xhr.open("get", "http://www.baidu.com");......xhr.send(null);实现跨浏览器的CORS
function createCORS(method, url){var xhr = new XMLHttpRequest();if("withCredentials" in xhr){xhr.open(method, url, true);}else if(typeof XDomainRequest != "undefined"){var xhr = new XDomainRequest();xhr.open(method, url);}else{xhr = null;}return xhr;}var request = createCORS("get", "http://www.baidu.com");if(request){request.onload = function(){......};request.send();}7> JSONP
function handleResponse(response){console.log("The responsed data is: "+response.data);}var script = document.createElement("script");script.src = "http://www.baidu.com/json/?callback=handleResponse";document.body.insertBefore(script, document.body.firstChild);/*handleResonse({"data": "zhe"})*///原理如下://当我们通过script标签请求时//后台就会根据相应的参数(json,handleResponse)//来生成相应的json数据(handleResponse({"data": "zhe"}))//最后这个返回的json数据(代码)就会被放在当前js文件中被执行//至此跨域通信完成jsonp虽然很简单,但是有如下缺点:
var socket = new WebSockt("ws://www.baidu.com");//http->ws; https->wsssocket.send("hello WebSockt");socket.onmessage = function(event){var data = event.data;}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。