javascript - 将当前页面 url 传递给 iframe 地址

标签 javascript html iframe

你好,我正在尝试得到类似的东西

<iframe src="http://anydomain.com/frame.php?ref=http://currentpageurl.com/dir"></iframe>

我正在使用此 HTML

<iframe src="http://localhost/
<script type="text/javascript">
document.write(window.location.pathname);
</script>
.html" frameborder="0" scrolling="no" onload="resizeIframe(this)" />

我在控制台中收到此错误

Failed to load resource: the server responded with a status of 403 (Forbidden)
GET http://localhost/%3Cscript%20type=/

最佳答案

对于一个纯粹的 JS 解决方案,这似乎就是你想要的(尽管我建议在服务器端渲染期间这样做),我会这样做:

<script type="text/javascript">
      var iframe = document.createElement('iframe');
      iframe.src = 'http://anydomain.com/frame.php?ref=' + window.location.href; // if you just want the path change `href` to `pathname`
      document.body.appendChild(iframe); // insert the element wherever you want in the page
</script>

关于javascript - 将当前页面 url 传递给 iframe 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852170/

相关文章:

javascript - 如何从 json 文件中选择并打印值

javascript - Chrome 是否尝试将 "unbundle"JavaScript 源代码放入其原始文件中以在调试器中显示?

javascript - 当页面完全加载完成时显示 Webview?

javascript - 如何限制字段仅接受 4 个数字字符作为输入并在 div 中获取警报

javascript - 选择没有元素环绕的内容

javascript - 使用javascript动态更改html元素的位置

html - 当 iFrame 从本地 html 文件加载本地 html 文件时,Chrome 的 future 版本是否可能支持 contentWindow/contentDocument?

javascript - 为什么 HTML 表单无法加载到 Javascript 创建的 iframe 中?

javascript - 查找 ul 如果它的父元素是 li

html - (HTML & CSS) 不会让我改变 iframe 的大小