Loading External JS File onClick not working in Firefox

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;
                        
                        if(document.all)
                        {
                              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>

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

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

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
LVL 4
vladimir_kalashnikovAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZylochCommented:
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;
    document.getElementsByTagName("head")[0].appendChild(s);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vladimir_kalashnikovAuthor Commented:
That fixed it, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.