I have a widget that I have hosted. I am able to successfully call it, and it loads. But When I am trying to pass it params from browser its not loading. The main difference is how I am calling the script.
The following code works but then its hard coded params
<div id="idealy_widget"></div>
<link href="https://xxxxxxxx/jscript/index.css" rel="stylesheet"/>
<script id="widget-params" src="https://xxxxxxxxx/jscript/index.js"
clientId="xxxxxxxxxxxxxxxxxxxxx"
clientSecret="xxxxxxxxxx"
username="xxxxxxxxxxxxx"
product_id="xxxxxxxxxx" type="module">
</script>
The following code doesnt work. it doesnt show the form which is part of the widget. It print the params.
<div id="idealy_widget"></div>
<script type="module">
const clientId = new URLSearchParams(window.location.search).get('clientId');
const clientSecret = new URLSearchParams(window.location.search).get('clientSecret');
const username = new URLSearchParams(window.location.search).get('username');
const product_id = new URLSearchParams(window.location.search).get('product_id');
console.log('Params:', clientId, clientSecret, username, product_id);
const widgetScript = document.createElement('script');
widgetScript.src = `https://xxxxxxxxxxxxx/jscript/index.js?clientId=${clientId}&clientSecret=${clientSecret}&username=${username}&product_id=${product_id}`;
widgetScript.type = 'module';
widgetScript.id = 'widget-params';
widgetScript.onload = () => {
console.log('Script loaded successfully');
// You may not need to manually call initWidget since it's called internally in index.js
};
document.head.appendChild(widgetScript);
</script>
2
Answers
I think your problem is how to transform params form a script typeof module
That’s probably because you made it do something completely different, than what the original code contained.
You are adding all those parameters to the script URL here – but that is not what was in the original code. In that, these were set as attributes on the
script
element – so presumably, you need to add them to your dynamically added script element, in exactly the same way. (Unless you found it documented somewhere, that this was also supposed to work the same way, with URL parameters?)