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.