Detect iframe src change using jquery

对着背影说爱祢 提交于 2019-12-12 20:18:01

问题


Lets say an <iframe> is initially loaded pointing to a certain src. The user clicks on a hyperlink and the contents of the iframe changes.

I would like to detect that new src of the iframe in full.

Unfortunately it appears to only provide the original src when a change is detected. It detects the change, just not what it changed to.

Perhaps it is a browser-specific issue. I am using chrome.

$(function(){
    $('iframe').load(function() {
        var src = $("iframe").attr('src')
        alert(src);
    });
});

http://jsfiddle.net/vud8S/7/


回答1:


Fortunately it is not possible to get an URL of an iframe from a different domain than it's parent page. You will not be able to read the full url.

If you try to read the documents location (of an iframe whose src is a differnt domain) than you will get a permission error that looks similar to this: "Error: Permission denied to get property Location.href"

This is due to "Same Origin Policy". More details on that: http://de.wikipedia.org/wiki/Same-Origin-Policy



来源:https://stackoverflow.com/questions/20128651/detect-iframe-src-change-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!