Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-16-2003, 08:30 AM   PM User | #1
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
hiding javascript!

Yes, it's possible! ...at least in IE, using external javascript and server-side language.
In my code below, I used VBScript/ASP. You can convert it to any server-side language.

js.asp
Code:
/*****************************
JavaScript Code Hider/Blocker
Created by: Glenn G. Vergara
July, 2003
glenngv@yahoo.com
Philippines
******************************/
<%
'don't cache the js file
Response.Buffer = True
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "cache-control", "no-store"
Response.ContentType = "text/javascript"

Dim strReferer, bAccessOK, strRefererHost, intStart, intFirstSlash, bViewJS, strMyHost, strProtocol

bAccessOK = false
bViewJS = true
strReferer = request.servervariables("HTTP_REFERER")

if strReferer <> "" then
	intStart = instr(1,strReferer,"://")
	intFirstSlash = instr(intStart+3,strReferer,"/") 
	strRefererHost = mid(strReferer,intStart+3,intFirstSlash-intStart-3)
	strMyHost = request.servervariables("HTTP_HOST")
	if strRefererHost = strMyHost then bAccessOK = true
	bViewJS = false
end if

if not bAccessOK then
	if bViewJS then
%>
alert("Sorry, you are not allowed to view my JavaScript codes!!!");
<%
	else 'other site has linked this js file
		if request.servervariables("HTTPS")="on" then strProtocol = "https" else strProtocol = "http"
%>
alert("Sorry, this site is NOT allowed to link the JavaScript file, <%=strProtocol & "://" & strMyHost & request.servervariables("URL")%> !!!");
<%
	end if
	Response.End
end if
%>
/********************************
End JavaScript Code Hider/Blocker
*********************************/

//your javascript codes start here
alert('You can execute my JavaScript codes!');//test
just use it in any page like this:
<script type="text/javascript" src="js.asp"></script>

The code above doesn't only hide the js file, it also prevents other sites to link the file.

With this code, the js file is not cached on the client's temporary internet files, so you can't view its source. Typing view-source:url of js file in the address bar will just generate the "You are not allowed..." message. You can't even download the file.
But unfortunately, that "view-source" behavior is in IE only, though in other browsers, the js file is also not cached. In NS6+ or Mozilla, using view-source:url of js file will display the actual generated source code of the js. In NS4, the server variable that holds the url of the referring document returns empty. It can be concluded that when NS4 accesses the external js file in a page, the referer is always empty. Maybe it can also be said for accessing images, css files in a page. So, it's totally unusable in NS4.

I posted it even though it has limited browser support. I just want to point out that it is still possible to hide javascript codes from the client. We all know that we have never-ending questions of that nature here in CodingForums.

Your comments are welcome.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 07-16-2003, 03:16 PM   PM User | #2
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
The directive to not cache can be altered by firewall software and despite that directive I think IE will put it into the cache and then delete the file as you leave the page so as long as the user has the page open they can still find the JS file in the cache.
__________________
Check out the Forum Search. It's the short path to getting great results from this forum.
Roy Sinclair is offline   Reply With Quote
Old 07-16-2003, 03:36 PM   PM User | #3
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
I agree - it will have no effect because the file will be cached, even if only temporarily. All one would have to do is open the cache folder; or of course, not use IE to view it.
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark
brothercake is offline   Reply With Quote
Old 07-16-2003, 04:52 PM   PM User | #4
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
I guess I should have added that while it's an interesting thought and a nice effort it's still a not worth bothering to use since it's still easily bypasses.

Fortunately or unfortunately (depending on your point of view) the very nature of how the web works makes these efforts into exercises in futility.
__________________
Check out the Forum Search. It's the short path to getting great results from this forum.
Roy Sinclair is offline   Reply With Quote
Old 07-16-2003, 05:20 PM   PM User | #5
whackaxe
Senior Coder

 
Join Date: Jun 2002
Location: paris, france
Posts: 1,216
Thanks: 0
Thanked 0 Times in 0 Posts
whackaxe is an unknown quantity at this point
yeah people can rip you, but why make it easy for them?
__________________
photoshop too expensive? use the GIMP! www.gimp.org
whackaxe is offline   Reply With Quote
Old 07-16-2003, 08:40 PM   PM User | #6
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
Those who don't know how to go around a few simple barriers probably aren't smart enough to be able to make much if any use of code worth the effort of putting barriers around. Those who are smart enough to make use of the code will also be smart enough to be able to find their way around your barriers.
__________________
Check out the Forum Search. It's the short path to getting great results from this forum.
Roy Sinclair is offline   Reply With Quote
Old 07-17-2003, 02:37 AM   PM User | #7
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
I'm not saying that it's a fool-proof solution. And I'm not really a "hide javascript" advocate either. As I said in the last part of my post, I just pointed out that it is possible to hide the code in a specific browser such as IE. I just toyed with this solution because of all those countless "hide javascript" posts here in CF and other forums.
I have tested it in our intranet and it works, the js file is not cached. I guess it may be useful in an intranet where the environment is controlled and the only browser is IE. And even then, you have to know where and when to use it or not (at all). I'm not in any way encouraging users to hide all their javascript codes in their applications. Only in justifiable situations such as simple online quizzes, games, etc...
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 08-07-2003, 08:05 PM   PM User | #8
Drakain Zeil
New Coder

 
Join Date: Jul 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Drakain Zeil is an unknown quantity at this point
You could always try to chipper it, thou, I don't think that would go over too well.
Drakain Zeil is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:12 AM.


Advertisement
Log in to turn off these ads.