JavaScript¿çÓò×ܽáÓë½â¾ö°ì·¨
±¾ÎÄÀ´×ÔÍøÂ磨http://f2e.me/200904/cross-scripting/£¬¸ÃÍøÖ·ÒѲ»ÄÜ·ÃÎÊ£©£¬½ö×÷¸öÈ˶ÁÊé±Ê¼ÇÖ®Ó㬲¢ÉÔ×÷Ð޸ĺͲ¹³ä¡£
ʲôÊÇ¿çÓò
JavaScript³öÓÚ°²È«·½ÃæµÄ¿¼ÂÇ£¬²»ÔÊÐí¿çÓòµ÷ÓÃÆäËûÒ³ÃæµÄ¶ÔÏó¡£µ«ÔÚ°²È«ÏÞÖƵÄͬʱҲ¸ø×¢Èëiframe»òÊÇajaxÓ¦ÓÃÉÏ´øÀ´Á˲»ÉÙÂé·³¡£ÕâÀï°ÑÉæ¼°µ½¿çÓòµÄһЩÎÊÌâ¼òµ¥µØÕûÀíһϣº
Ê×ÏÈʲôÊÇ¿çÓò£¬¼òµ¥µØÀí½â¾ÍÊÇÒòΪJavaScriptͬԴ²ßÂÔµÄÏÞÖÆ£¬a.com ÓòÃûϵÄjsÎÞ·¨²Ù×÷b.com»òÊÇc.a.comÓòÃûϵĶÔÏ󡣸üÏêϸµÄ˵Ã÷¿ÉÒÔ¿´ÏÂ±í£º
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 |
²»Í¬ÓòÃû | ²»ÔÊÐí |
- Ìرð×¢ÒâÁ½µã£º
- µÚÒ»£¬Èç¹ûÊÇÐÒéºÍ¶Ë¿ÚÔì³ÉµÄ¿çÓòÎÊÌ⡰ǰ̨¡±ÊÇÎÞÄÜΪÁ¦µÄ£¬
- µÚ¶þ£ºÔÚ¿çÓòÎÊÌâÉÏ£¬Óò½ö½öÊÇͨ¹ý¡°URLµÄÊײ¿¡±À´Ê¶±ð¶ø²»»áÈ¥³¢ÊÔÅжÏÏàͬµÄipµØÖ·¶ÔÓ¦×ÅÁ½¸öÓò»òÁ½¸öÓòÊÇ·ñÔÚͬһ¸öipÉÏ¡£
¡°URLµÄÊײ¿¡±Ö¸[removed].protocol +[removed].host£¬Ò²¿ÉÒÔÀí½âΪ¡°Domains, protocols and ports must match¡±¡£
½ÓÏÂÀ´¼òµ¥µØ×ܽáÒ»ÏÂÔÚ¡°Ç°Ì¨¡±Ò»°ã´¦Àí¿çÓòµÄ°ì·¨£¬ºǫ́proxyÕâÖÖ·½°¸Ç£Éæµ½ºǫ́ÅäÖã¬ÕâÀï¾Í²»²ûÊöÁË£¬ÓÐÐËȤµÄ¿ÉÒÔ¿´¿´yahooµÄÕâƪÎÄÕ£º¡¶[removed] Use a Web Proxy for Cross-Domain XMLHttpRequest Calls¡·
1¡¢document.domain+iframeµÄÉèÖÃ
¶ÔÓÚÖ÷ÓòÏàͬ¶ø×ÓÓò²»Í¬µÄÀý×Ó£¬¿ÉÒÔͨ¹ýÉèÖÃdocument.domainµÄ°ì·¨À´½â¾ö¡£ ¾ßÌåµÄ×ö·¨ÊÇ¿ÉÒÔÔÚhttp://www.a.com/a.htmlºÍhttp://script.a.com/b.htmlÁ½¸öÎļþÖзֱð¼ÓÉÏ document.domain = ¡®a.com¡¯£»È»ºóͨ¹ýa.htmlÎļþÖд´½¨Ò»¸öiframe£¬È¥¿ØÖÆiframeµÄcontentDocument£¬ÕâÑùÁ½¸öjsÎļþÖ®¼ä¾Í¿ÉÒÔ ¡°½»»¥¡±ÁË¡£µ±È»ÕâÖÖ°ì·¨Ö»Äܽâ¾öÖ÷ÓòÏàͬ¶ø¶þ¼¶ÓòÃû²»Í¬µÄÇé¿ö£¬Èç¹ûÄãÒìÏëÌ쿪µÄ°Ñscript.a.comµÄdomianÉèΪalibaba.com ÄÇÏÔÈ»Êǻᱨ´íµØ£¡´úÂëÈçÏ£º
www.a.comÉϵÄa.html
document.domain = 'a.com'; var ifr = document.createElement('iframe'); ifr.src = 'http://script.a.com/b.html'; ifr.style.display = 'none'; document.body.appendChild(ifr); ifr.onload = function(){ var doc = ifr.contentDocument || ifr.contentWindow.document; // ÔÚÕâÀï²Ù×Ýb.html alert(doc.getElementsByTagName("h1")[0].childNodes[0].nodeValue); };
script.a.comÉϵÄb.html
document.domain = 'a.com';
ÕâÖÖ·½Ê½ÊÊÓÃÓÚ{www.kuqin.com, kuqin.com, script.kuqin.com, css.kuqin.com}ÖеÄÈκÎÒ³ÃæÏ໥ͨÐÅ¡£
±¸×¢£ºÄ³Ò»Ò³ÃæµÄdomainĬÈϵÈÓÚ[removed].hostname¡£Ö÷ÓòÃûÊDz»´øwwwµÄÓòÃû£¬ÀýÈça.com£¬Ö÷ÓòÃûÇ°Ãæ´øǰ׺µÄͨ³£¶¼Îª¶þ¼¶ÓòÃû»ò¶à¼¶ÓòÃû£¬ÀýÈçwww.a.comÆäʵÊǶþ¼¶ÓòÃû¡£ domainÖ»ÄÜÉèÖÃΪÖ÷ÓòÃû£¬²»¿ÉÒÔÔÚb.a.comÖн«domainÉèÖÃΪc.a.com¡£
- ÎÊÌ⣺
- 1¡¢°²È«ÐÔ£¬µ±Ò»¸öÕ¾µã£¨b.a.com£©±»¹¥»÷ºó£¬ÁíÒ»¸öÕ¾µã£¨c.a.com£©»áÒýÆð°²È«Â©¶´¡£
- 2¡¢Èç¹ûÒ»¸öÒ³ÃæÖÐÒýÈë¶à¸öiframe£¬ÒªÏëÄܹ»²Ù×÷ËùÓÐiframe£¬±ØÐ붼µÃÉèÖÃÏàͬdomain¡£
2¡¢¶¯Ì¬´´½¨script
ËäÈ»ä¯ÀÀÆ÷ĬÈϽûÖ¹ÁË¿çÓò·ÃÎÊ£¬µ«²¢²»½ûÖ¹ÔÚÒ³ÃæÖÐÒýÓÃÆäËûÓòµÄJSÎļþ£¬²¢¿ÉÒÔ×ÔÓÉÖ´ÐÐÒýÈëµÄJSÎļþÖеÄfunction£¨°üÀ¨²Ù×÷cookie¡¢DomµÈµÈ£©¡£¸ù¾ÝÕâÒ»µã£¬¿ÉÒÔ·½±ãµØͨ¹ý´´½¨script½ÚµãµÄ·½·¨À´ÊµÏÖÍêÈ«¿çÓòµÄͨÐÅ¡£¾ßÌåµÄ×ö·¨¿ÉÒԲο¼YUIµÄGet Utility
ÕâÀïÅжÏscript½Úµã¼ÓÔØÍê±Ï»¹ÊÇÂùÓÐÒâ˼µÄ£ºieÖ»ÄÜͨ¹ýscriptµÄreadystatechangeÊôÐÔ£¬ÆäËüä¯ÀÀÆ÷ÊÇscriptµÄloadʼþ¡£ÒÔÏÂÊDz¿·ÖÅжÏscript¼ÓÔØÍê±ÏµÄ·½·¨¡£
js.onload = js.onreadystatechange = function() { if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') { // callbackÔÚ´Ë´¦Ö´ÐÐ js.onload = js.onreadystatechange = null; } };
3¡¢ÀûÓÃiframeºÍlocation.hash
Õâ¸ö°ì·¨±È½ÏÈÆ£¬µ«ÊÇ¿ÉÒÔ½â¾öÍêÈ«¿çÓòÇé¿öϵĽŲ½Öû»ÎÊÌâ¡£ÔÀíÊÇÀûÓÃlocation.hashÀ´½øÐд«Öµ¡£ÔÚurl£º http://a.com#hellowordÖеġ®#helloworld¡¯¾ÍÊÇlocation.hash£¬¸Ä±ähash²¢²»»áµ¼ÖÂÒ³ÃæˢУ¬ËùÒÔ¿É ÒÔÀûÓÃhashÖµÀ´½øÐÐÊý¾Ý´«µÝ£¬µ±È»Êý¾ÝÈÝÁ¿ÊÇÓÐÏ޵ġ£¼ÙÉèÓòÃûa.comϵÄÎļþcs1.htmlÒªºÍcnblogs.comÓòÃûÏ嵀 cs2.html´«µÝÐÅÏ¢£¬cs1.htmlÊ×ÏÈ´´½¨×Ô¶¯´´½¨Ò»¸öÒþ²ØµÄiframe£¬iframeµÄsrcÖ¸Ïòcnblogs.comÓòÃûÏ嵀 cs2.htmlÒ³Ã棬ÕâʱµÄhashÖµ¿ÉÒÔ×ö²ÎÊý´«µÝÓá£cs2.htmlÏìÓ¦ÇëÇóºóÔÙ½«Í¨¹ýÐÞ¸Äcs1.htmlµÄhashÖµÀ´´«µÝÊý¾Ý£¨ÓÉÓÚÁ½¸öÒ³Ãæ²»ÔÚͬһ¸öÓòÏÂIE¡¢Chrome²»ÔÊÐíÐÞ¸Äparent.location.hashµÄÖµ£¬ËùÒÔÒª½èÖúÓÚa.comÓòÃûϵÄÒ»¸ö´úÀíiframe£»Firefox¿ÉÒÔÐ޸ģ©¡£Í¬Ê±ÔÚcs1.htmlÉϼÓÒ»¸ö¶¨Ê±Æ÷£¬¸ôÒ»¶Îʱ¼äÀ´ÅжÏlocation.hashµÄÖµÓÐûÓб仯£¬Ò»µãÓб仯Ôò»ñÈ¡»ñÈ¡hashÖµ¡£´úÂëÈçÏ£º
ÏÈÊÇa.comϵÄÎļþcs1.htmlÎļþ£º
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ÓòϵĴúÀíiframe
var 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);
µ±È»ÕâÑù×öÒ²´æÔںܶàȱµã£¬ÖîÈçÊý¾ÝÖ±½Ó±©Â¶ÔÚÁËurlÖУ¬Êý¾ÝÈÝÁ¿ºÍÀàÐͶ¼ÓÐÏ޵ȡ¡
4¡¢window.nameʵÏֵĿçÓòÊý¾Ý´«Êä
ÎÄÕ½ϳ¤ÁÐÔÚ´Ë´¦²»±ãÓÚÔĶÁ£¬ÏêϸÇë¿´ window.nameʵÏֵĿçÓòÊý¾Ý´«Êä¡£
5¡¢Ê¹ÓÃHTML5 postMessage
HTML5ÖÐ×î¿áµÄй¦ÄÜÖ®Ò»¾ÍÊÇ ¿çÎĵµÏûÏ¢´«ÊäCross Document Messaging¡£ ÏÂÒ»´úä¯ÀÀÆ÷¶¼½«Ö§³ÖÕâ¸ö¹¦ÄÜ£ºChrome 2.0+¡¢Internet Explorer 8.0+, Firefox 3.0+, Opera 9.6+, ºÍ Safari 4.0+ ¡£ FacebookÒѾʹÓÃÁËÕâ¸ö¹¦ÄÜ£¬ÓÃpostMessageÖ§³Ö»ùÓÚwebµÄʵʱÏûÏ¢´«µÝ¡£
- otherWindow.postMessage(message, targetOrigin);
- otherWindow: ¶Ô½ÓÊÕÐÅÏ¢Ò³ÃæµÄwindowµÄÒýÓ᣿ÉÒÔÊÇÒ³ÃæÖÐiframeµÄcontentWindowÊôÐÔ£»window.openµÄ·µ»ØÖµ£»Í¨¹ýname»òϱê´Ówindow.framesÈ¡µ½µÄÖµ¡£
message: ËùÒª·¢Ë͵ÄÊý¾Ý£¬stringÀàÐÍ¡£
targetOrigin: ÓÃÓÚÏÞÖÆotherWindow£¬¡°*¡±±íʾ²»×÷ÏÞÖÆ
a.com/index.htmlÖеĴúÂ룺
<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>
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>
²Î¿¼ÎÄÕ£º¡¶¾«Í¨HTML5±à³Ì¡·µÚÎåÕ¡ª¡ª¿çÎĵµÏûÏ¢»úÖÆ¡¢https://developer.mozilla.org/en/dom/window.postmessage
6¡¢ÀûÓÃflash
ÕâÊÇ´ÓYUI3µÄIO×é¼þÖп´µ½µÄ°ì·¨£¬¾ßÌå¿É¼ûhttp://developer.yahoo.com/yui/3/io/¡£
¿ÉÒÔ¿´ÔÚAdobe Developer Connection¿´µ½¸ü¶àµÄ¿çÓò´úÀíÎļþ¹æ·¶£ºross-Domain Policy File Specifications¡¢HTTP Headers Blacklist¡£
ÍƼöÐÅÏ¢
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- jQuery Tooltips²å¼þ
- jQueryµÄ.bind()¡¢.live()ºÍ.delegate(....
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- Ä㲻ϲ»¶»ðºü£¬µ«ÓÖϲ»¶firebug£¬ÄǾ͸úÎÒÒ»ÆðÀ´ÔÚIEÉÏ....
- ¸ù¾ÝÏÔʾÆ÷·Ö±æÂʵ÷ÕûÒ³ÃæÏÔʾµÄJavascript½Å±¾
- jQuery дµÄ·ÂÐÂÀË΢²© ÏòϹö¶¯Ð§¹û¡¾×ªÔØ¡¿
- ͨÓõÄJqueryÑ¡ÔñÆ÷
- jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬....
- ×ÔÖÆjQueryÖÇÄÜÌáʾ²å¼þһö
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- js»ñÈ¡srcÖд«µÝµÄ²ÎÊý
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- ÈÃä¯ÀÀÆ÷ÆÁ±ÎµôJavaScriptµÄ³ö´íÌáʾ
- github²»ÄܼÓÔØcss¡¢js½â¾ö°ì·¨
- ÎÒÖªµÀµÄJavaScript -- Éè¼Æģʽ(ÇŽÓ)Ó¦ÓÃÖ® ¨C ....
- 15¸öÖµµÃ¿ª·¢ÈËÔ±¹Ø×¢µÄjQuery¿ª·¢¼¼ÇɺÍÐĵÃ
- dz̸JavascriptÃæÏò¶ÔÏó±à³Ì
- JS¼òµ¥¶¯»·â×°
- JavaScript³õѧÕßӦעÒâµÄÆ߸öϸ½Ú
- jQuery Tooltips²å¼þ
ÆÀÂÛ