本來看著好玩的,在自己部落格上弄個一個tx微博,但是突然發現姓名和地址的寬度在google裡有點大,擠變形了,唉。。不想用css,css覆蓋啊啥的也不行啊,用js改變寬度。。結果悲劇的發現了這個js跨域問題
找資料。。未解決。。
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title> </title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
<table id="Calendar1_entryCal"><tr><td>234234234</td></tr></table>
<iframe name="fname" id="fname" frameborder="0" scrolling="no" src="http://show.v.t.qq.com/index.php?c=show&a=index&n=m184394282&w=0&h=508&fl=1&l=30&o=1&co=4&cs=4252_202_9C85_BC6" width="100%" height="400"></iframe>
<div id="wb"></div>
</body>
</html>
<script type="text/javascript">
window.onload =function () {
// var test = $("#Calendar1_entryCal");
// test.hide();
// test.before().append
// $("#userInfo + p").removeAttr("style");
// $(window.frames["fname"].document).find(".userInfo:eq(0) > p").removeAttr("style");
alert($(document.getElementById('fname').contentWindow.document.body).html());//無存取權限
alert($(window.frames["fname"].document).html());
alert($(window.frames["fname"].document).find($(".userInfo:eq(0) > p")));
alert($(window.frames["fname"].document).find($(".userInfo:eq(0) > p")).attr("style"));
}
</script>
方案一、剪貼簿
原理:IE本身依附於windows平台的特性為我們提供了一種基於iframe,利用記憶體來“繞行”的方案,在這裡我稱之為,本機存放區原理。
缺點:不支援非IE瀏覽器,並且影響到使用者對剪貼簿的操作,使用者體驗非常不好,特別是在IE7下,受安全等級影響,會彈出提示框。
示範:[ 點此閱覽 ]
子頁面在子域:demo.ioldfish.cn下,在頁面中加入如下代碼擷取頁面高度並存入剪貼簿。
- <script type="text/javascript">
- var ua = navigator.userAgent;
- if ((i = ua.indexOf("MSIE")) >= 0)
- {
- var iObjH = window.document.documentElement.scrollHeight;
- window.clipboardData.setData('text',String(iObjH));
- }
- </script>
首頁面在主域:www.ioldfish.cn下,在頁面中加入如下代碼,擷取剪貼簿的值並賦值為子頁面所在的iframe的高度。
- <script type="text/javascript">
- window.onload = function()
- {
- var iObj =document.getElementById('iId');
- iObj.style.height=parseInt(window.clipboardData.getData('text'))+'px';
- }
- </script>
方案二、document.domain
原理:設定了document.domain,欺騙瀏覽器
缺點:無法實現不同主域之間的通訊。並且當在一個頁面中還包含有其它的iframe時,會產生安全性異常,拒絕訪問。
示範:[ 點此閱覽 ]
首頁面在主域:www.ioldfish.cn下,子頁面在子域:demo.ioldfish.cn下,在兩個頁面的頭部都加入如下代碼:
- <script type="text/javascript">
- document.domain="ioldfish.cn";
- </script>
方案三、通過JS擷取hash值實現通訊
原理:hash可以實現跨域傳值實現跨域通訊。
缺點:對於父視窗URL參數動態產生的,處理過程比較複雜一些。並且IE之外的瀏覽器遇到hash的改變會記錄曆史,影響瀏覽器的前進後退功能,體驗不佳。
示範:[ 點此閱覽 ]
子頁面在主域:www.lzdaily.com下,在頁面中添加如下代碼,因為hash是不受跨域限制的,所以可以將本頁面的高度順利的傳送給首頁面的hash。
- <script type="text/javascript">
- var hashH = document.documentElement.scrollHeight;
- var urlA = "http://www.ioldfish.cn/wp-content/demo/domain/hash/a2.html";
- parent.location.href= urlA+"#"+hashH;
- </script>
首頁面在主域:www.ioldfish.cn下,在頁面中添加如下代碼,首先取得子頁面傳遞過來的hash值,然後將hash值賦到子頁面所在的iframe的高度上。
- <script type="text/javascript">
- window.onload = function()
- {
- var iObj = document.getElementById('iId');
- if(location.hash)
- {
- iObj.style.height=location.hash.split("#")[1]+"px";
- }
- }
- </script>
方案四、傳hash值實現通訊改良版
原理:該方案通過“前端代理”的方式,實現hash的傳值,體驗上比之方案三有了很大的提升。
缺點:對於父視窗URL參數動態產生的,處理過程比較複雜一些。
示範:[ 點此閱覽 ]
子頁面在主域:www.lzdaily.com下,在頁面中添加如下代碼,首先在頁面裡添加一個和首頁面同域的iframe[也可動態產生],他的作用就像是個跳板。C頁面中不需任何代碼,只要確保有個頁面就防止404錯誤就可以了!該方法通過修改iframe的name值同樣可以跨域傳值,只是比較”猥瑣“而已。
- <iframe id="iframeC" name="iframeC" src="http://www.ioldfish.cn/wp-content/demo/domain/hashbetter/c.html" frameborder="0" height="0"></iframe>
然後在頁面中加上如下代碼,利用頁面C的URL接收hash值,並負責把hash值傳給同域下的首頁面。
- <script type="text/javascript">
- hashH = document.documentElement.scrollHeight;
- urlC = "http://www.ioldfish.cn/wp-content/demo/domain/hashbetter/c.html";
- document.getElementById("iframeC").src=urlC+"#"+hashH;
- </script>
首頁面在主域:www.ioldfish.cn下,在頁面中添加如下代碼,擷取從C頁面中傳遞過來的hash值。這裡應用到一個技巧,就是直接從A頁面用frames["iId"].frames["iframeC"].location.hash,可以直接存取並擷取C頁面的hash值。這樣一來,通過代理頁面傳遞hash值,比起方案三,大大提高了使用者體驗。
- <script type="text/javascript">
- window.onload = function()
- {
- var iObj = document.getElementById('iId');
- iObjH = frames["iId"].frames["iframeC"].location.hash;
- iObj.style.height = iObjH.split("#")[1]+"px";
- }
- </script>