0

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?

3
  • 1
    On what event does this need to occur on page load, on clicking a link, ... ? Commented Jul 6, 2021 at 13:40
  • @JasperB after loading the page (if it detects id in the wordpress url then this id must be added to the url of iframe) Commented Jul 6, 2021 at 13:43
  • 1
    ok, ill change the example I just submitted Commented Jul 6, 2021 at 13:46

1 Answer 1

0

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.