【问题标题】:How to use variable as src iframe in wordpress?如何在wordpress中使用变量作为src iframe?
【发布时间】:2021-07-06 13:29:27
【问题描述】:

我想设置一个动态url作为变量,我试过这种方式但是不行:

<iframe src=src width="100" height="100"></iframe>

<script>
var src = "https://mypage.com"
console.log(src)
</script>

有可能吗?

【问题讨论】:

  • 在页面加载、点击链接时需要发生什么事件...?
  • @JasperB 加载页面后(如果它在wordpress url中检测到id,则必须将此id添加到iframe的url中)
  • 好的,我把刚刚提交的例子改一下

标签: wordpress iframe dynamic src


【解决方案1】:

加载页面的示例

function setIframeSrc(id, url) {
    document.getElementById(id).src = url;
}
document.addEventListener("DOMContentLoaded", function() {
    var urlParams = new URLSearchParams(window.location.search)
    if(urlParams.get('id') != null){
       var id = urlParams.get('id')
       setIframeSrc('target_frame', 'https://example.com?id' + id)
    }
});
&lt;iframe id="target_frame" src="about:blank" style="width:500px; height:500px;"&gt;&lt;/iframe&gt;

【讨论】:

    猜你喜欢
    • 2017-05-21
    • 2013-04-11
    • 1970-01-01
    • 2012-01-28
    • 2017-04-15
    • 2021-11-25
    • 2011-03-20
    • 2022-11-17
    • 2016-12-06
    相关资源
    最近更新 更多