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 6861729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:34:20+00:00 2026-05-27T02:34:20+00:00

What I want to do is to separate all my JavaScript from the page,

  • 0

What I want to do is to separate all my JavaScript from the page, so it works for all with disabled JavaScript, but also be able to compile it using closure compiler’s advanced compiling (http://closure-compiler.appspot.com/home).

Here is how I started:

From…

<a href="javascript:doMultiple('download', 'release');">Create archive</a>

to…

<a class="javascript doMultiple download release" href="ca.php">CreateArchive</a>

Then I loop all objects to find what have the class javascript first, etc: (links is an array of the a tags)

for (i = 0; i < links.length; i = i + 1) {
    if (links[i].className.substr(0, 10) === "javascript") {
        jsArray = links[i].className.split(" ");
        links[i].style.display = "inline";
        if (links[i].addEventListener) {
            links[i].addEventListener("click", window[jsArray[1]], false);
        } else {
            links[i].onclick = window[jsArray[1]];
        }
        links[i].href = "#";
    }
}

With this method javascript compressors will remove the function doMultiple because it’s unused because it dosnt know I have a class with that…

And my question is, how can I solve this, and if I can’t, can I somehow get rid of the window[jsArray[1]] and do solve that in another way? Because I think that is not the best way to do it.

A class can also be:

<a class="javascript popUp register.php 350 300" href="register.php">Register</a>

So I want to use this method for adding all JavaScript dynamically.

Best regards,
Johan Svensson

  • 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-05-27T02:34:20+00:00Added an answer on May 27, 2026 at 2:34 am

    You shouldn’t need to make things complicated. It’s better to attach an event to your DOM element.

    I would highly suggest you to use a JavaScript framework like jQuery, or even you can achieve this using plain JavaScript.

    For instance

    <a href="#" id="createArchive" class="remove otherClass">Create archive</a>
    

    OR

    <a href="#" id="createArchive" class="release otherClass">Create archive</a>
    

    And for JavaScript:

    var element = document.getElementById("createArchive");
    
    var handler = function() {
            // this line will execute only on IE
            // you need an if-else case to test
            // against the browser other than IE
            // and use this.class.split
            var classes = this.className.split(' ');
    
            doMultiple('download', classes[0]);
            // or anything you want to on this click event
    };
    
    if (element && element.attachEvent) {
        // This is for IE
        element.attachEvent("onclick", handler);
    }
    else if (element && element.addEventListener) {
        // This is for rest of the world
        element.addEventListener("click", handler, false);
    }
    

    And if you use jQuery, life is simple

    $("#createArchive").bind("click", function() {
        doMultiple('download', 'release');
    });
    

    That’s it, and also mind that there’s no JavaScript code in your HTML.

    Question 2

    For Google Closure Compiler, you have to use exclude the function that you don’t want to be compiled/minified.

    You should have a .JS file which will contain the function names to be excluded.

    Your exclude.js should contain the signature of the functions like:

    function doMultiple() { }
    function popUp() { }
    

    And run this from your command line to compile using closure compiler

    java -jar .\compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js input_script_file.js --js_output_file=compiled.min.js --externs exclude.js
    

    Hope this help. Please feel free to ask any question

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

Sidebar

Related Questions

I want to be able to write javascript that executes on a page that
I want to create 2 separate pickers in the same view using the same
I want to keep SSH debug info separate (and logged) from other input. However,
What if you don't want to start a separate project for grails but instead
I'd like to display some language specific characters from javascript but I can't. My
I am using jQuery autocomplete, and want to have a separate PHP file to
I want JavaScript code to be separated from views. I got the requirement to
I'm working with a VB.Net page which calls a web method from JavaScript, this
I want to have multiple Facebook Like Buttons on a single page, all pointing
I have the simplest code that I want to separate in three files: Header

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.