在IE中动态更改iframe之后获取iframe的src值

时间:2020-12-25 15:13:06

After I update an iframe's src attribute using : document.getElementById('iframe1').src = newsrc and then attempt to read it: alert(document.getElementById('iframe1').src); The src I get is the old source and not the new one that was set. Why is this and how can I get the real current src value?

在我使用:document.getElementById('iframe1')。src = newsrc更新iframe的src属性之后再尝试读取它:alert(document.getElementById('iframe1')。src);我得到的src是旧源,而不是新设置的源。为什么这样,我怎样才能得到真正的当前src值?

2 个解决方案

#1


2  

If the src is pointing to a different domain, you can't get it. It's a security feature. I guess IE's way of implementing that security feature is to return the old value for src.

如果src指向不同的域,则无法获取它。这是一个安全功能。我猜IE实现该安全功能的方法是返回src的旧值。

Correction: After reading Pekka's comments, I went ahead and tested it. You can try it too at http://jsfiddle.net/pMtZC/ (caution: alerts).

更正:在阅读了Pekka的评论之后,我继续进行测试。您也可以在http://jsfiddle.net/pMtZC/(警告:警告)中尝试。

On all browsers I tested it in (Chromium, Firefox 4, IE 8), the changed src property was returned just fine. If you set it, it will return the value you set to it. It will not, however, change if the user navigates within the iframe (obviously). What version of IE are you using?

在我测试的所有浏览器(Chromium,Firefox 4,IE 8)中,更改后的src属性返回正常。如果设置它,它将返回您为其设置的值。但是,如果用户在iframe内导航(显然),它就不会改变。您使用的是哪个版本的IE?

#2


-2  

Trick it into thinking its not on the local domain.

欺骗它认为它不在本地域。

#1


2  

If the src is pointing to a different domain, you can't get it. It's a security feature. I guess IE's way of implementing that security feature is to return the old value for src.

如果src指向不同的域,则无法获取它。这是一个安全功能。我猜IE实现该安全功能的方法是返回src的旧值。

Correction: After reading Pekka's comments, I went ahead and tested it. You can try it too at http://jsfiddle.net/pMtZC/ (caution: alerts).

更正:在阅读了Pekka的评论之后,我继续进行测试。您也可以在http://jsfiddle.net/pMtZC/(警告:警告)中尝试。

On all browsers I tested it in (Chromium, Firefox 4, IE 8), the changed src property was returned just fine. If you set it, it will return the value you set to it. It will not, however, change if the user navigates within the iframe (obviously). What version of IE are you using?

在我测试的所有浏览器(Chromium,Firefox 4,IE 8)中,更改后的src属性返回正常。如果设置它,它将返回您为其设置的值。但是,如果用户在iframe内导航(显然),它就不会改变。您使用的是哪个版本的IE?

#2


-2  

Trick it into thinking its not on the local domain.

欺骗它认为它不在本地域。