views:

35

answers:

1

Hi

I'm trying to make a little script that'll inject a tag in a page.

Here it is:

    <script type="text/javascript" charset="utf-8">
    var url = 'http://my_url.com/js/widget_rss.js?cle=4c65683101e22&amp;host=' + window.location.hostname;
    var scriptTag = document.createElement('script')
    scriptTag.src = url;
    var head = document.getElementsByTagName("head")[0];
    (head || document.body).appendChild(scriptTag);
    </script>

It is working, it calls the other file and the file runs but it makes the browser load forever...

Any ideas what's wrong about it?

Thanks!

+1  A: 

Every time I need this kind of script loading I use this snippet:

function loadScript(scriptName) {
    var customScript = document.createElement('script');
            customScript.type = 'text/javascript';
            customScript.async = true;
  customScript.src = scriptName;
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(customScript);
};//loadScript

After this, you just call with loadScript('path/to/script').

If you need more advanced stuff (like callback and CSS loaders) you can use lazyload.

Ionut Staicu
I do almost the same thing as your snippet. But I still tried it and it does exactly the same thing... :/ thanks tho
Tom
Then the problem may be the script you trying to load :)
Ionut Staicu