【问题标题】:Access iFrame URL with JQuery / Javascript使用 JQuery / Javascript 访问 iFrame URL
【发布时间】:2011-04-11 23:02:17
【问题描述】:

我有一个这样的页面...

<div>
<iframe id="mainContent">
<iframe id="littleBox">
</div>

我想这样做......

 $(".someButton").live('click', function() {
       alert(/*The URL That the Main Frame is ON*/);
 });

我找到了这个:$("#mainFrame").get(0).location.href,但它不起作用...

另外,我需要它来返回当前 url,所以如果有人在它周围导航应该返回他们所在的当前页面。

【问题讨论】:

标签: javascript jquery iframe


【解决方案1】:

好的,您的意思是您必须从子 iframe 访问父级的 URL?

$("#mainFrame").get(0).contentWindow.location

【讨论】:

  • 父框架(在我的示例中为未命名的 div)需要访问子 iframe 的 CURRENT url。问题是,在导航到与子 iframe 的起始 url 不同的 url 后,我仍然得到子 iframe 的原始起始 URL。
【解决方案2】:

iframe.src 不工作吗?或者,您是否正在尝试获取父文档?在这种情况下,您可以使用parent

【讨论】:

    【解决方案3】:

    正如 Azmisov 所说,iframe.src 可以解决问题

    <head runat="server">
        <title>iframe Test</title>
        <script type="text/javascript">
            function ShowURLs() {
                var control = document.getElementById("mainContent");
                alert("iframe source = " + control.src);
                alert("this page = " + location.href);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input type="button" id="someButton" value="Click Me" onclick="ShowURLs()" />
            <div>
                <iframe id="mainContent" src="Test.aspx" />
                <iframe id="littleBox" />
            </div>
        </div>
        </form>
    </body>
    

    【讨论】:

    • 不,它没有。 iframe.src 返回原始网址。在您的情况下,即使您离开 Test.aspx,它也会返回 Test.aspx 作为 src。只需将 Test.aspx 上的链接添加到 SomethingElse.aspx,然后再次单击该按钮...所需的输出将是 SomethingElse.aspx。
    【解决方案4】:
    document.getElementById('mainContent').contentWindow.location.href
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多