user2818060
user2818060

Reputation: 845

Hinding Js in Viewsource is not working

what I need

Debug

Upvotes: 0

Views: 321

Answers (2)

osuddeth
osuddeth

Reputation: 162

You can't truly hide your js code. You can obfuscate it (i.e. make it difficult to read), but unlike PHP or Perl - which is processed on the server side - JS runs in the client's browser itself. Therefore, the client always has a copy of it, and can view that source at any time.

Upvotes: 0

John Doherty
John Doherty

Reputation: 4105

Use the online Google Closure Compiler service, it will make your code almost unreadable by doing things like renaming variables and function names. For example:

Raw JS

function toggleDisplay(el){
    if (!el) return;
    el.style.display = (el.style.display==='none') ? 'block' : 'none';
}

Closure Compiled

function toggleDisplay(a){a&&(a.style.display="none"===a.style.display?"block":"none")};

JavaScript Beautified

function toggleDisplay(a){
    a&&(a.style.display="none"===a.style.display?"block":"none")
};

In doing so it also reduces the size of your script, helping to boost the loading time of your webpage.

You can still read the script, but its harder to understand and can get really complex when using things like JavaScript Closures.

Upvotes: 1

Related Questions