javascript - 在 iframe.src 和 iframe.contentwindow.location 之间进行选择的函数

标签 javascript jquery iframe

我正在使用 iframe.get(0).contentWindow.location 来获取 iframe 的当前 url。问题是,如果用户导航离开原始域,则会引发错误。我如何告诉 js/jquery 使用 iframe.src 代替?

TIA!

最佳答案

您需要尝试获取contentWindow位置:

try {src = iframe.get(0).contentWindow.location.href;}
catch(e) {src = iframe.get(0).src;}

关于javascript - 在 iframe.src 和 iframe.contentwindow.location 之间进行选择的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17146099/

相关文章:

javascript - Jquery 在复选框上切换两个 div

javascript - jQuery .live ('click' ) 没有按预期工作

jquery - 动画延迟,但在页面加载/函数触发时立即启动

javascript - 如果验证失败,Jquery 验证将不起作用

javascript - jQuery 功能荧光笔插件

HTTPS 和框架。连接是否加密?

javascript - 等待的 promise 仍在返回 <pending>

javascript - "this"将函数绑定(bind)到 Javascript 类中的事件时不起作用

javascript - 阻止用户在 Iframe 中导航

javascript - 使用javascript嵌入动态大小的网页