基于自定义 WordPress 元数据的动态 iframe src

Dynamic iframe src based on custom WordPress metadata

我一直在尝试在 WordPress 模板中显示图表。 它适用于这种格式:

<iframe height="500" src="https://datasource.com/fantom/0xf704f5ac5edf152168e07e6f5f108366911250ac?embed=1&theme=dark&trades=0&info=0">
</iframe>

但我似乎无法使用动态元数据使其在模板中工作。

<script>
function ShowGraph() {
    var RawNetwork=document.getElementById("TokenNetwork").innerHTML;
    var Network = RawNetwork.replace(/<[^>]*>/g, '');
    
    var RawAddress=document.getElementById("TokenAddress").innerHTML;
    var Address = RawAddress.replace(/<[^>]*>/g, '');
    
    const Graph = 'https://datasource.com/' + Network + '/' + Address + '?embed=1&theme=dark&trades=0&info=0';
    document.getElementById('TokenGraph').src = Graph;
}
</script>

<iframe  height="500" id="TokenGraph" src="" onLoad="ShowGraph();"></iframe>

我希望有人能帮我弄清楚我在这里遗漏了什么。

朋友们干杯!

有循环。 Onload 您正在更改 src,然后 onload 再次触发。

尝试

<script>
window.onload = function () {
    var RawNetwork=document.getElementById("TokenNetwork").innerHTML;
    var Network = RawNetwork.replace(/<[^>]*>/g, '');

    var RawAddress=document.getElementById("TokenAddress").innerHTML;
    var Address = RawAddress.replace(/<[^>]*>/g, '');

    const Graph = 'https://datasource.com/' + Network + '/' + Address + '?embed=1&theme=dark&trades=0&info=0';
    document.getElementById('TokenGraph').src = Graph;
}
</script>
<iframe  height="500" id="TokenGraph" src=""></iframe>