Search code examples
wordpressiframedynamicsrc

How to use variable as src iframe in wordpress?


I want to set a dynamic url as a variable, I tried it this way but it doesn't work:

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

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

Is it possible?


Solution

  • An example for loading the page

    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)
        }
    });
    <iframe id="target_frame" src="about:blank" style="width:500px; height:500px;"></iframe>