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

时间:2016-04-25 22:24:34

标签: 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=/

1 个答案:

答案 0 :(得分:2)

对于纯粹的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>