需要将HTML参数传递给iframe

nedd to pass html parameter to iframe

本文关键字:iframe 参数传递 HTML      更新时间:2023-09-26

我有一个接受参数的页面:http://example.com/mypage.html?param=123

在html中,我有一个iframe需要这个参数,就像这样:

& lt; iframe的src = " http://example.com/myapp?id=123 "等等…>

现在我知道如何从url解析参数,我可以在onload()中更改iframe的src。但问题是iframe首先显示w/o参数(显示一些默认的iframe),然后在onload后它将正确显示。

我可以在加载时显示带有解析参数的iframe吗?(没有php,只有javascript)

看起来解决方案是像这样在正文中添加javascript:

<script>
    var sid = location.search.substr(location.search.indexOf("sid=")+4);
    var ifrm = document.getElementById('myiframe') ;
    var target = "http://example.com/myApp?sid=" + sid;
        ifrm.setAttribute('src', target);       
</script>