---
title: "window.opener.location.href  跨域访问 在IE6，IE7，IE8不兼容"
author: "wangyi529"
date: "2010-07-07T16:39:10Z"
tags: []
categories: ["Web前端"]
slug: "window-opener-location-href-ie6-ie7-ie8"
draft: false
lastmod: "2011-01-08T09:23:34Z"
id: 707614
url: "https://529.iteye.com/blog/707614"
---

场景如下：
**www.a.com下有一个页面a.html(http://www.a.com/a.html)，页面的代码如下：**

```html

<html>
<head>
<title>a.html</title>
</head>
<body>
<a href="http://www.b.com/b.html" target="_blank">b.html _blank</a><br>
<a href="#" onclick="window.open ('http://www.b.com/b.html/')">b.html window.open</a>
</body>
</html>

```


**www.b.com下有一个页面a.html(http://www.b.com/b.html)，页面的代码如下：**

```html

<html>
<head>
<title>b.html</title>
<script>
if(window.opener!=null){
var wuri = window.opener.location.href ;
var q=getQuery(wuri);//获取父窗体地址栏的某一个参数
window.close();
window.opener.location = "http://www.b.com/login.html"
}else{
window.location = "http://www.b.com/login.html"
}
</script>
</head>
<body>
this is the b.html
</body>
</html>

```


**以上的代码，当显示b.html时，在IE7，IE8下都没有问题，而在IE6下有js错误。
各种测试之后，发现跨域是，IE6不允许读取 window.opener.location.href 的值。
为了让以上代码不出现js错误，暂时把b.html更改为如下：**

```html

<html>
<head>
<title>b.html</title>
<script>
try{
	window.opener.location.href;
}catch(e){
	window.opener=null;
}
if(window.opener!=null){
var wuri = window.opener.location.href ;
var q=getQuery(wuri);//获取父窗体地址栏的某一个参数
window.close();
window.opener.location = "http://www.b.com/login.html"
}else{
window.location = "http://www.b.com/login.html"
}
</script>
</head>
<body>

this is the b.html
</body>
</html>

```

不知道各位仁人志士，有没有不更改原来逻辑的情况下，解决这个问题。
谢谢