How do I hide javascript code in a webpage?

前端 未结 11 726
时光取名叫无心
时光取名叫无心 2020-11-22 17:17

Is it possible to hide the Javascript code from the html of a webpage, when the source code is viewed through the browsers View Source feature?

I know it is possibl

相关标签:
11条回答
  • 2020-11-22 17:57

    Put your JavaScript into separate .js file and use bundling & minification to obscure the code.

    http://www.sitepoint.com/bundling-asp-net/

    0 讨论(0)
  • 2020-11-22 18:01

    Is not possbile!

    The only way is to obfuscate javascript or minify your javascript which makes it hard for the end user to reverse engineer. however its not impossible to reverse engineer.

    0 讨论(0)
  • 2020-11-22 18:03

    'Is not possible!'

    Oh yes it is ....

    //------------------------------
    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]);
        }
      }      
    }
    
    0 讨论(0)
  • 2020-11-22 18:04

    No, it isn't possible.

    If you don't give it to the browser, then the browser doesn't have it.

    If you do, then it (or an easily followed reference to it) forms part of the source.

    0 讨论(0)
  • 2020-11-22 18:05

    I think I found a solution to hide certain JavaScript codes in the view source of the browser. But you have to use jQuery to do this.

    For example:

    In your index.php

    <head>
    <script language = 'javascript' src = 'jquery.js'></script>
    <script language = 'javascript' src = 'js.js'></script>
    </head>
    
    <body>
    <a href = "javascript:void(null)" onclick = "loaddiv()">Click me.</a>
    
    <div id = "content">
    </div>
    
    </body>
    

    You load a file in the html/php body called by a jquery function in the js.js file.

    js.js

    function loaddiv()
    {$('#content').load('content.php');}
    

    Here's the trick.

    In your content.php file put another head tag then call another js file from there.

    content.php

    <head>
    <script language = 'javascript' src = 'js2.js'></script>
    </head>
    
    <a href = "javascript:void(null)" onclick = "loaddiv2()">Click me too.</a>
    
    <div id = "content2">
    </div>
    

    in the js2.js file create any function you want.

    example:

    js2.js

    function loaddiv2()
    {$('#content2').load('content2.php');}
    

    content2.php

    <?php
    echo "Test 2";
    ?>
    

    Please follow link then copy paste it in the filename of jquery.js

    http://dl.dropbox.com/u/36557803/jquery.js

    I hope this helps.

    0 讨论(0)
提交回复
热议问题