0

what I need

  • I need to hide js code in view source js code

        function unloadJS(scriptName) {
        var head = document.getElementsByTagName('head').item(0);
        var js = document.getElementById(scriptName);
        js.parentNode.removeChild(js);
        }
    
        function unloadAllJS() {
        var jsArray = new Array();
        jsArray = document.getElementsByTagName('script');
        for (i = 0; i < jsArray.length; i++){
        if (jsArray[i].id){
        unloadJS(jsArray[i].id)
        }else{
        jsArray[i].parentNode.removeChild(jsArray[i]);
        }
        }       
        }
    
    
            var page_count = {{count()}};
            if (page_count == 4)
            {
            dataLayer.push({'event':'mobilePromo-android'});
            }
        $(document).ready(function()
        {
            var page_count = {{count()}};
            var height= $(window).height();
            if (page_count == 4 )
            {
    
                $.ajax({
                    type: "GET",
                    url: "http://times.com/mobilepopuptracker?from=android", 
    
                });
    
                $('body').html('<div class="row flush aligncenter popbx" style="height:'+height+'px"><div class="12u">');               
    
            }
            else
            {
    
            }
    
    
        });
            function redirect()
            {
    
                  var a=$(location).attr('href');
                   window.location.href=a;
            }
    
        </script>
    

    Problem

    • I Need to hide js code in view source.

Debug

  • i have reffred the link find solution on http://www.sitepoint.com/hide-jquery-source-code/.

  • though code is still viewed.

  • any suggestion are most welcome.

  • though we know we cannot stop viewing of js in view source but still there must be some trick.

user2818060
  • 835
  • 5
  • 19
  • 41
  • I believe hiding JS is not (fully) possible but some "strong" minification can be as good too ;) – nettutvikler Jan 08 '15 at 14:21
  • 1
    Anybody with a little development experience will be able to read your code. Who else would care to view it? Why do you want this? Hopefully not for security. – Ruan Mendes Jan 08 '15 at 14:53

2 Answers2

1

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.

John Doherty
  • 3,669
  • 36
  • 38
  • 2
    Even if you do that, you can just run it through a beautifier and the code will be almost as readable except for renamed variables – Ruan Mendes Jan 08 '15 at 14:54
  • Yes but its much harder to understand with single letter variable and function names. It does a few other things like merging arrays and changing things like **var found=false** into **var f=!-1** – John Doherty Jan 08 '15 at 15:19
  • 1
    Sure, I still say it's a futile exercise. Your example shows that the un-minified code is not that hard to read for anyone with decent knowledge of the language – Ruan Mendes Jan 08 '15 at 18:26
0

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.

osuddeth
  • 152
  • 9