There is another way to hide Javascript for the most basic users.
Just check here to try to find javascript behind the text box ...
However, the script is still displayed for advanced users - see the bottom of this post to see why -
The idea is to put your javascript functions in a separate .js file. When loading the source PHP or HTML page instead of directly calling with
<SCRIPT language="JavaScript" SRC="original_file_to_hide.js"></SCRIPT>
you include the php script header, which will copy the “mysource.js” file to the random file “kcdslqkjfldsqkj.js” and modify your HTML file to call
<SCRIPT language="JavaScript" SRC="temporary_copy_of_the_file.js"></SCRIPT>
instead of this. After that, simply delete the copy kcdslqkjfldsqkj.js file on your server, and when the user searches for the source code, the browser will contact the missing file !!!
So, this is for theory, then there is a small problem to work around : if the HTML / PHP file loads too quickly, your script will be deleted from your server before the browser has loaded the script.
So you need
- To copy a file to another random name
- Upload file to PHP source file
- Wait a few seconds after loading the HTML / PHP file until ...
- ... delete file
Here is the HTML / PHP source " test.php " that should be displayed to the end user:
<?php //javascript source code hiding technique : Philippe PUECH, 2013 //function thanks to Stackoverflow, slightly modified //http://stackoverflow.com/questions/4356289/php-random-string-generator function RandomString() { $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $randstring = ''; for ($i = 0; $i < 10; $i++) { $randstring = $randstring.$characters[rand(0, strlen($characters))]; } return $randstring; } //simple header script to create a copy of your "precious" javascript ".js" file $original_filename="functions.js"; //find a better (complicated) name for your file $hidden_filename=RandomString().".js"; //temporary filename copy($original_filename,$hidden_filename); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Catch my Javascript if you can !</title> </head> <SCRIPT language="JavaScript" SRC="<?php echo($hidden_filename); ?>"></SCRIPT> <script type="text/javascript"> </script> <body onLoad="javascript:testfunc();"> This is the page with anything you like ! </body> </html> <?php sleep(1); //you can comment following line echo "finished !"; unlink($hidden_filename); ?>
Here is the source of the functions.js file that will be hidden from the user.
// JavaScript Document function testfunc(){ alert("It works..."); }
However, as stated in the comment, the browser developer tools will store the script in memory and make it still visible to curious users ...; - ((