Loading External JS File onClick not working in Firefox

Posted on 2006-03-30
Last Modified: 2008-01-16
I have 4 images that when clicked load an external javascript file onto the page.  Here is the code for it:

                 <script language="javascript">
                  function returnObj(name)
                        var obj = null;
                              obj = document.all[name];
                        else if(document.getElementById)
                              obj = document.getElementById(name);
                        else if(document.layers)
                              obj = document.layers[name];
                        return obj;

                  function switchText(name)
                        var js = returnObj('remoteJS');

                        js.src = 'test'+name+'.js';

            <script language='javascript' id='remoteJS'>

            <a onclick="javascript:switchText('one');" style='cursor:pointer'>
                  <img src='img1.jpg'>
            <a onclick="javascript:switchText('two');" style='cursor:pointer'>
                  <img src='img1.jpg'>
            <a onclick="javascript:switchText('three');" style='cursor:pointer'>
                  <img src='img1.jpg'>
            <a onclick="javascript:switchText('four');" style='cursor:pointer'>
                  <img src='img1.jpg'>

In the external js files I have some alerts just to test if it works:

                 alert('test 1');

So all of this works correctly in IE, but in Firefox it does nothing.  I am wondering what the difference is.  Is this a security issue (loading from an external file)?  This is probably an easy question, but I rarely use javascript and hardly ever from an external file.  Any help would be appreciated.

- vlad
Question by:vladimir_kalashnikov
    LVL 36

    Accepted Solution

    I cannot tell you why this is, as I am no expert at browser specifics, but I would expect it to be because FF only executes <script> tag when it's added, not when its src is changed (although I could be wrong).

    Anyhow, a way to solve that, at least for FF is this:

    function loadScript(url)
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.language = "javascript";
        s.src = url;
    LVL 4

    Author Comment

    That fixed it, thanks.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now