鍍金池/ 問(wèn)答/HTML/ js中關(guān)于window.name的疑問(wèn)。

js中關(guān)于window.name的疑問(wèn)。

http://b.com/a.html:

<iframe src="http://b.com/b.php" onload="a && alert(a)"></iframe>

http://b.com/b.php:

<?php
echo "<script>
         name = 'a';
      </script>";
?>

訪問(wèn):http://b.com/a.html
如下:a && alert(a) 執(zhí)行到了alert(a)卻報(bào)錯(cuò)。

clipboard.png

回答
編輯回答
孤島

file:///文件訪問(wèn)協(xié)議的origin 為null,也就是沒(méi)有域的,存在跨域訪問(wèn)的問(wèn)題
a通過(guò)window.name賦值,是一個(gè)指向iframe這個(gè)widow的一個(gè)引用
當(dāng)需要訪問(wèn)a指向的window對(duì)象的屬性時(shí),就會(huì)報(bào)跨域錯(cuò)誤

2017年6月30日 14:11
編輯回答
玩控

放到本地服務(wù)器上例如,localhost://,你這里直接文件打開(kāi)的是 file 協(xié)議,所以報(bào)錯(cuò)跨域了

2017年5月31日 02:15
編輯回答
愿如初

補(bǔ)充貼圖
clipboard.png
a && alert(a) 中a沒(méi)有報(bào)跨域錯(cuò)誤,alert(a)卻報(bào)跨域錯(cuò)誤。
@xianshenglu

2017年11月7日 09:24