Peter
Peter

Reputation: 11835

JS Closure Compiler - Dont change the function-names


i use the http://closure-compiler.appspot.com/home to compress my code.

MY JS File *EDIT:*

function test_it()
{
 // some code
}

MY HTML Code

<div onclick="test_it" />test</div>

My problem is that the compressor rename my function.

When i click (after the compression) on the div i get the error "test_it is not defined". That makes sense coz the function has a new "short" name now.

Is it possible to deactivate "in the Advanced mode" the "rename functions" function? Or is there a other solution?

Thanks in advance!
Peter

Upvotes: 0

Views: 320

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039080

Your function has errors in it. You have a closing ) at the end and a ; which is not necessary. It should be:

function test_it()
{
    // some code
}

Now if you use the Simple mode you won't have any problems. Be careful with the Advanced mode though as it might perform optimizations and remove this function if not used.

Upvotes: 1

Magnar
Magnar

Reputation: 28830

The solution is to bind the click event through javascript:

function test_it() {
    // some code
}
document.getElementById("test").onclick = test_it;

HTML:

<div id="test">test</div>

This way the compiler will still change the name of the function (saves space), but it will also change the name of the reference -> problem solved.

Upvotes: 1

Related Questions