Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Loading External JS File onClick not working in Firefox

Posted on 2006-03-30
Medium Priority
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

Zyloch earned 500 total points
ID: 16338990
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;

Author Comment

ID: 16344824
That fixed it, thanks.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

572 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