dongrong3171 2014-01-18 17:52
浏览 65
已采纳

如何根据父窗口的URL更改iFrame的scr域?

How can I change the scr="" attribute of an iFrame based on the current window's URL? Here's an example of what I'm trying to do: If the parent window's URL is "/i/foo" the iFrame's scr should be "/i/new/foo". If it's "/i/cake" then the scr needs to be "/i/new/cake". If the parent window's URL is just "/i" then the iFrame's scr needs to be "/i/new".
How can I achieve this using php, javascript, or some other method?
I believe there are some issues when using php with an iframe.

  • 写回答

1条回答 默认 最新

  • dsoy71058 2014-01-18 18:01
    关注

    You can put some javascript in the top level page that can examine the URL of its page and then set the URL of the iframe:

    var iframeURL = window.location.href.replace(/\/[^\/]+\/?$/, "/new$&");
    document.getElementById("myIframe").src = iframeURL;
    

    This finds the last segment of the URL and then inserts /new in front of it and then finds the iframe and sets it's .src property. You would need to insert this script AFTER the iframe object in the page (or wait for the document to be loaded before running the script) and you would need to attach an id to the iframe so it can be found.

    <iframe id="myIframe"></iframe>
    <script>
    var iframeURL = window.location.href.replace(/\/[^\/]+\/?$/, "/new$&");
    document.getElementById("myIframe").src = iframeURL;
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog