简体   繁体   English

将当前页面网址传递到iframe地址

[英]pass current page url to iframe address

Hello I'm trying to get something like 您好,我正在尝试获得类似

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

I'm using this HTML 我正在使用此HTML

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

I'm getting this error in console 我在控制台中收到此错误

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

For a purely JS solution which seems to be what you're going for (though I'd recommend doing this during server-side rendering instead) I'd do something like this: 对于似乎是您要使用的纯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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM