Adding javascript widget code in the right direction

I am providing javscript code to clients that adds a widget to their site. Currently I'm asking them to insert this right above the tag </body>

:

<script type="text/javascript">
var myHost = (("https:" == document.location.protocol) ? "https://" : "http://");
document.write(unescape("%3Cscript src='" + myHost + "www.mywebsite.com/mycode.js.php?u="+encodeURIComponent(window.location.host)+"' type='text/javascript'%3E%3C/script%3E"));
</script>

      

As you can see, the code calls my script under the http or https url depending on the page url.

Sometimes when the HTML of the site is not well formed, the script can lead to an "aborted" error in IE browsers.

I want to send a client code to be attached to a scope <head>

.

  • How can I handle http / https when calling code from the main part?
  • Do you know if it will fix the canceled operation problem?
0


a source to share


2 answers


Just give them an HTTPS solution.

If their page is secure, it will work fine; If not, there is nothing wrong with embedding a protected object on an unsafe page.



<script type="text/javascript" src="https://www.mywebsite.com/mycode.js.php"></script>

      

I can see that you are passing the original domain to the querystring. You can get this from your JS code without having to pass it through the GET parameter. You can check it using the property document.referrer

.

+2


a source


It looks like Google handles its analytics code the exact same way (which I'm sure you know). What are you doing in your code? When I looked at the "operation aborted" link you provided, it says that this is a solution to execute your code on page load.



I would recommend bind the function to the onload event and execute your DOM code there.

+2


a source







All Articles