I have an ASP.NET Webforms app where I want to invoke a JavaScript file in another domain and pass it some query strings. I know that I could just use
<script id="myscript" src="https://localhost/MyProject/Scripts/Script.js?queryString1=Result1&QueryString2=Result2"></script>
from the hosting app and inside the script search for it using its ID and get the src, but there is another trick.
In fact, I’m invoking the script in the following function from, lets say, http://localhost/MyApp/Start.aspx
, and every time I get the window.location.href
or document.URL
values inside the script, they all point to http://localhost/MyApp/Start.aspx
instead of http://localhost/MyProject/myscript.js?query1=Result1
.
(function (w, d, s, o, f, js, fjs) {
w['MyWidget'] = o; w[o] = w[o] || function () { (w[o].q = w[o].q || []).push(arguments) };
js = d.createElement(s), fjs = d.getElementsByTagName(s)[0];
js.id = o; js.src = f; js.async = 1; fjs.parentNode.insertBefore(js, fjs);
}(window, document, 'script', 'mw', "http://localhost/MyProject/Scripts/Script.js?QueryString1=Result1"));
mw('init', { showButton: true });
Actually, MyProject
is a simple HTML widget that I want to add to any website (here, for example, MyApp
), and the query strings are some configuration settings. I tried with global variables from MyApp
and they work, i.e. I can access them from MyProyect
, but I’d prefer to pass them just in one line, without having the user to add more JavaScript code to his/her website.
Any ideas or suggestions are welcome.