How to Protect Javascript Code From Copying by Ajax Method

by admin admin Date: 24-06-2013 javascript ajax jquery websecurity script tutorial


Today, in this mini tutorial we will introduce a simple method to protect javascript code from copying.

IMPORTANT: We are not sure this method will protect javascript 100% but it will be better than do nothing ;-D

As we said, this method is very simple: basically we have to combine AJAX method with little server-side script to prevent visitors access javascript file directly.

Let's explain our method:

		
 	<!DOCTYPE html>
 
 	<html>
 
 	<head>
 
 	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
 	<title>Protecting Javascript Code From Copying</title>
 
 	 
 
 	<!-- jQuery Library 1.8.3 -->
 
 	<script type="text/javascript" src="js/jquery/jquery.min.183.js"></script>
 
 	 
 
 	<script type="text/javascript">
 
 	 
 
 	$(document).ready(function(e) {   
 
 	  function MyJSFunction() {
 
 	    alert("This is my script, you can not copy this!");
 
 	  }
 
 	  // Show Alert
 
 	  MyJSFunction();  
 
 	});
 
 	</script>
 
 	</head>
 
 	 
 
 	<body>
 
 	  Protecting Javascript Code From Copying.
 
 	</body>
 
 	</html>
						
					
	

 

So, we have a javascript function called MyJSFucntion that we don't want users view it.

When users access the page they will see an alert message. Then users want to see the javascript code that is used to trigger the alert message so they will use “view page source” or “inspect element” or “firebug”.

But the question is we don't want anyone see our javascript code so we'll put the javascript code into a php file ( js-protect.php ).

Then we use an AJAX method to load the php file.

js-protect.php content:

		
 	<script type="text/javascript">
 
 	function MyJSFunction() {
 
 	  alert("This is my script, you can not copy this!");
 
 	}  
 
 	// Show Alert
 
 	MyJSFunction();
 
 	</script>
						
					
	

The AJAX code to load js-protect.php file in “js/” folder ( we use jQuery ):

		
 	<script type="text/javascript">
 
 	$(window).load(function() {
 
 	  $.ajax({
 
 	    url: 'js/js-protect.php',
 
 	    type:'POST',
 
 	    cache: false,
 
 	    success: function(data){
 
 	      if(data){
 
 	        $('body').append(data);
 
 	      }
 
 	    }
 
 	  });
 
 	})
 
 	</script>
						
					
	

 

Now, when users view source code of the page they will not see the javascript we wrote.

But, some experient user may be know the link of js.php file by which they can access the file directly.

To protect javascript from this "intrusion" we will use a simple validating method to check referer link. If the referer link is the index page we will allow them to access the javascript code otherwise we will show them another content.

Changing the js-protect.php file:

		
 	<?php
 
 	  $referer = $_SERVER['HTTP_REFERER'];
 
 	  // Check URL
 
 	  if ( $referer=='http://ma-no.org/free/demo/demo-protect.php' ) {
 
 	?>
 
 	 
 
 	<script type="text/javascript">
 
 	function MyJSFunction() {
 
 	  alert("This is my script, you can not copy this!");
 
 	}  
 
 	// Show Alert
 
 	MyJSFunction();
 
 	</script>
 
 	 
 
 	<?php }
 
 	// If strange url
 
 	else { ?>
 
 	    Sorry! You can not see this content. Please go to <a href="http://ma-no.org">MA_NO.ORG</a><br />
 
 	<?php } ?>
						
				

The index page ( demo.php ):

		
 	<!DOCTYPE html>
 
 	<html>
 
 	<head>
 
 	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
 	<title>Protecting Javascript Code From Copying</title>
 
 	 
 
 	<!-- jQuery Library 1.8.3 -->
 
 	<script type="text/javascript" src="js/jquery/jquery.min.183.js"></script>
 
 	 
 
 	<script type="text/javascript">
 
 	$(window).load(function() {
 
 	  $.ajax({
 
 	    url: 'js/js-protect.php',
 
 	    type:'POST',
 
 	    cache: false,
 
 	    success: function(data){
 
 	      if(data){
 
 	        $('body').append(data);
 
 	      }
 
 	    }
 
 	  });
 
 	})
 
 	</script>
 
 	</head>
 
 	 
 
 	<body>
 
 	  Protecting Javascript Code From Copying.
 
 	</body>
 
 	</html>
						
					
	

 

Our work is completed!

 
by admin admin Date: 24-06-2013 javascript ajax jquery websecurity script tutorial hits : 6663  
 
 
 
 

Related Posts