這篇文章主要為大家詳細介紹了jQuery用iframe來解決跨域的簡單示例(1),具有一定的參考價值,可以用來參考一下。
感興趣的小伙伴,下面一起跟隨512筆記的小編羅X來看看吧。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * iframe來解決跨域 * * @param * @arrange (512.筆記) www.512pic.com **/ var iframe1 = document.createElement( "iframe" ); iframe1.src = "http://127.0.0.1/server.html" ; ( function () { // 當iframe加載完之后觸發的函數 function iframe1_load() { } // 在IE下要用attachEvent來添加iframe的onload事件 if (iframe1.attachEvent) { iframe1.attachEvent( "onload" , function (){ iframe1_load(); }); } else { iframe1.onload = iframe1_load; } })(); document.body.appendChild(iframe1); |
1 2 3 4 5 6 7 8 9 | /** * iframe來解決跨域 * * @param * @arrange (512.筆記) www.512pic.com **/ function iframe1_load() { alert(iframe1.contentWindow.name); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * iframe來解決跨域 * * @param * @arrange (512.筆記) www.512pic.com **/ var iframe1 = document.createElement( "iframe" ); iframe1.style.display = "none" ; document.body.appendChild(iframe1); ( function () { var same_domain = false ; // 當iframe加載完之后觸發的函數 function iframe1_load() { if (same_domain) { // 取得從服務器返回的數據 alert(iframe1.contentWindow.name); // 關閉iframe1的窗口 iframe1.contentWindow.close(); // 移除iframe1 document.body.removeChild(iframe1); } else { same_domain = true ; // 不能用iframe1.src = "empty.html",在IE下有錯誤 iframe1.contentWindow.location = "empty.html" ; } } // 在IE下要用attachEvent來添加iframe的onload處理函數 if (iframe1.attachEvent) { iframe1.attachEvent( "onload" , function () { iframe1_load(); }); } else { iframe1.onload = iframe1_load; } })(); iframe1.src = "http://127.0.0.1/server.html" ; |
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns = "http://www.w3.org/1999/xhtml" > < head > < title >Server</ title > < script type = "text/javascript" > window.name = "HELLO WORLD"; </ script > </ head > < body > </ body > </ html > |
注:關于jQuery用iframe來解決跨域的簡單示例(1)的內容就先介紹到這里,更多相關文章的可以留意512筆記的其他信息。關鍵詞:iframe,跨域