Code to direct to correct embed url

I would like to create code that pulls the Organisation from a Users Profile page, say “OrgCode” and uses that parameter within an if statement embed url such as below:

var OrgCode = Pulled from the “OrgCode” field within the Users Profile collection

if ( OrgCode === 1)
{ }

else if ( OrgCode === 2)
{ }

else
{}

Please could you advise what I am missing, in particular how to write the code to get the variable OrgCode and whether the embed code would work as written. Thanks in advance.
Sean

2 Likes

Hi Sean,
You need to use html code element.
Try this sample example:

<html>
<head>
    <script type="text/javascript">
        window.onmessage = (event) => {
            if (event.data) {
                const iframe = document.getElementById("myIframe");
                iframe.src = event.data;
            }
        };
    </script>
</head>
<body>
<iframe id="myIframe" src="https://www.yourSite.com"></iframe>
</body>
</html>

In order to pass the information to the html element you need to use postMessage method.

 $w("#yourHtmlElement").postMessage("https://www.yourOtherSite.com"); 

For more Information read here
Feel free to paste your code here.
Good luck!
Roi

Thank you for your quick reply Roi.
Where would I put the variable “OrgCode” in the script above?

Ive tried this with some advice within the page code. Unfortunately it isnt working. Please could you check to see what is missing? Thanks in advance.

(function () {
function createIFrame() {
var OrgCode = document.getElementById(‘input1’);
var iFrame = document.getElementById(‘OrgFrame’);
var targetSrc = OrgCode.value;
switch (targetSrc) {
case ‘TEMP1’:
iFrame.src = ‘URL1’;
break;
case ‘TEMP2’:
iFrame.src = ‘URL2’
break;
default:
iFrame.src = ‘URL3(which would include the OrgCode value)’;
}

} 
window.addEventListener('DOMDocumentLoaded', createIFrame); 

}());

Thanks Roi. Ive taken a look at the link for post message and am still struggling to understand how it should be actually written in the HTML embed code and in the page code. Would you be able to help me with the actual text for both?

Awesome complete example that works. I am looking for something similar to the reverse. I need an example of updating the parent window location but when the src URL changes. Any chance that’s easy for you?