Identical functions in multiple loaded scripts - choosing which version is called

Posted on 2006-03-29
Last Modified: 2008-02-20
I've got a single page for editing various sets of data; the user chooses which set to work with from a menu, and the page loads the list of stuff & textboxes, etc, into a container div on that page via xmlhttp. There is one universal "save" button that simply calls "save()".

Each editing module has its own .js file with native functions and a "save()" function defined. Since the content & scripts are loaded dynamically, if the user chooses 3 or 4 different options, each of those .js files gets loaded and the "save" functions keep getting overwritten. Since javascript uses the last defined version of a function this causes problems if they go back to a previously loaded module.

So basically my question is, how can I reload or reposition scripts in such a way that an already-loaded script's function becomes the "last defined" version of that function?

Here's a pseudocode example:

function loadScript(filename)
  if(document.scripts[filename]) return, else document.scripts += new script(filename)

function onMenuChange(newModule)
since each module has its own "save" function, this happens

onMenuChange(moduleA) // calls the "Save" function from moduleB.js
Question by:noamattd
    LVL 23

    Expert Comment

    by:Jens Fiederer
    One solution is NOT to use globally scoped save functions.

    If you make the Save function a method of an object (or methods of several objects), then multiple Save functions
    can coexist.

    You can then call either


    depending on which is appropriate.
    LVL 4

    Author Comment

    I thought about that.  I'm doing this with .NET and each module is a composite server control, as is the universal save button (and other buttons with similar features); the goal being to generate the universal buttons once, and not have to mess with them any more.

    If I were to have save be the method of different objects I would have to change the event handlers for each button every time a new module was loaded, and I was hoping to avoid that.
    LVL 23

    Accepted Solution

    You could have the event handlers call a function that uses a global object.  This global object would contain whatever you want to be the "last defined" object, and the handler could call g_functions.Save()
    LVL 4

    Author Comment

    That sounds more like what I had in mind, could you give an example?
    LVL 4

    Author Comment

    Never mind, I got it.  Thanks a bunch.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    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.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    In Part 1 ( 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'…
    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…

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now