Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:12:01+00:00 2026-06-04T17:12:01+00:00

In my HTML-head i have this script included: <script id=mode type=text/javascript src=article.js></script> With a

  • 0

In my HTML-head i have this script included:

<script id="mode" type="text/javascript" src="article.js"></script>

With a button click I’d like to change the source of the script to customers.js so that it then looks like this:

<script id="mode" type="text/javascript" src="customers.js"></script>

The point is that I don’t want the article.js to be included in my page then anymore, so I can’t just use .append().

So, click on the article button -> only article.js included, click on the customers button -> only customers.js included.

I tried to solve this with jQuery this way, but I doesn’ seem to work…:

$("#btArticle").click(function(){
  $("#mode").attr("src","article.js");
});

$("#btCustomers").click(function(){
  $("#mode").attr("src","customers.js");
});

Do you know where my mistake is?

Update: There are methods with the same name in customers.js and article.js. So there’s a onSave() method in both of them and when I clicked the customer button before, I want the onSave() method of customers.js to be executed, not the one in articles.js.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:12:05+00:00Added an answer on June 4, 2026 at 5:12 pm

    The point is that I don’t want the article.js to be included in my page then anymore, so I can’t just use .append().

    Once the script has been downloaded and evaluated, anything it leaves lying around will remain unless explicitly removed; they’re not linked to the script element itself and removing it won’t have any effect on them.

    The only way to get rid of the stuff article.js leaves lying around is to remove or overwrite each and every thing it creates and keeps.

    Concrete example:

    // article.js
    var foo = "bar";
    jQuery(function($) {
        $(".stuff").click(function() {
            alert("You clicked stuff!");
        });
    });
    

    If the article.js listed above is processed, you can remove the script element that loaded it, and that will have no effect on the foo global variable or the event handler that it hooked up.

    If you want to have scripts that you can unload, have them use the module pattern with a single global symbol they add by assigning to a property on window, e.g.:

    // article.js
    window.articleScript = (function() {
        var foo = "bar";
        jQuery(function($) {
            $(".stuff").bind("click.article", function() {
                alert("You clicked stuff!");
            });
        });
    
        function remove() {
            $(".stuff").unbind("click.article");
            try {
                delete window.articleScript;
            }
            catch (e) { // Early IEs throw incorrectly on the above
                window.articleScript = undefined;
            }
        }
    
        return {
            remove: remove
        };
    })();
    

    You can then remove it by doing this:

    articleScript.remove();
    

    Re your comment on the question:

    Maybe I should’ve mentioned that there are methods in both files with the same name.

    If you have global function declarations in customers.js that use the same name as global function declarations in articles.js, when you load customers.js, it will replace those functions.

    So if you have this in articles.js:

    function foo() {
        alert("Articles!");
    }
    

    …and this in customers.js:

    function foo() {
        alert("Customers!");
    }
    

    And you have a button:

    <input type="button" onclick="foo();" value="Foo">
    

    When you’ve loaded just articles.js and not customers.js, clicking that button gives you “Articles!”. If you then load customers.js, clicking the button will give you “Customers!”.

    That works because the event handler calls foo(), but the event handler itself is not foo. The onclick attribute creates a hidden event handler for you. The equivalent jQuery would be:

    $("input[type='button'][value='Foo']").click(function() {
        foo();
    });
    

    Note that just doing .click(foo) will do something very different: It will hook up the function that foo points to at that moment as the event handler. Even if you change what foo points to later (by loading customers.js), that won’t change the fact that the old function is hooked up as a handler.

    FWIW, from the question and your comments, I think I’d recommend sitting back and reviewing your strategy for this page/app. All of this swapping of code in and out and such seems like a design problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK i have this code HTML: <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js> </script> <script type=text/javascript>
I have this html code <html> <head> <title>JQuery Problem 2</title> <script type=text/javascript src=jquery-1.4.min.js></script> <script
Ok, I have this code: <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script> <script type=text/javascript> function get()
I have a html like this: <html> <head> <link rel=stylesheet type=text/css media=all href=style.css> </head>
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have used 3 some jquery related files namely <script type=text/javascript src=/blog/jquery/jquery-1.7.1.min.js></script> <link type=text/css
I have some HTML Code: <html> <head> <title>css test</title> <style type=text/css> .box{width:100%;float:left;background:red} </style> </head>
I have this Haml view: !!! strict %html{:xmlns => http://www.w3.org/1999/xhtml} %head %meta{:content => text/html;
I have simplified my page and here what I have: <html> <head> <meta http-equiv=content-type
I have a script foo.js that is included in <head> . Inside body I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.