跨域类型:这里暂且分为两种:主域相同,子域不同的跨域和完全跨域
1 2 3 4 5 6
|
<iframe id="iframe" src="http://child.domain.com/b.html"></iframe> <script> document.domain = 'domain.com'; var user = 'admin'; </script>
|
http://example.com/x.html#fragment的#fragment。如果只是改变片段标识符,页面不会重新刷新
1 2 3
|
var src = originURL + '#' + data; document.getElementById('myIFrame').src = src;
|
1 2 3 4 5 6 7
|
window.onhashchange = checkMessage;
function checkMessage() { var message = window.location.hash; }
|
1 2
|
parent.location.href= target + "#" + hash;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
window.onmessage = function(e) { if (e.origin !== 'http://bbb.com') return; var payload = JSON.parse(e.data); switch (payload.method) { case 'set': localStorage.setItem(payload.key, JSON.stringify(payload.data)); break; case 'get': var parent = window.parent; var data = localStorage.getItem(payload.key); parent.postMessage(data, 'http://aaa.com'); break; case 'remove': localStorage.removeItem(payload.key); break; } };
|
这篇
,原生js实现:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
<script> var script = document.createElement('script'); script.type = 'text/javascript';
script.src = 'http://www.domain2.com:8080/login?user=admin&callback=onBack'; document.head.appendChild(script);
function onBack(res) { alert(JSON.stringify(res)); } </script>
|
jQuery实现:
1 2 3 4 5 6 7
|
$.ajax({ url: 'http://www.domain2.com:8080/login', type: 'get', dataType: 'jsonp', jsonpCallback: "onBack", data: {} });
|
浏览器同源政策及其规避方法 - 阮一峰
前端常见跨域解决方案(全)
10种跨域解决方案