Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - Can browser handle a disposable JS file?

I want to hide the source of JS from direct access. So I thought to produce a disposable JS file. I use a temporary session cookie. The cookie is set before including JS file and expires then.

rather than the idea itself, my exact question is that how a browser manipulate the JS file? Does it cache a copy of JS file as soon as we write <script> tag or the script tag is just a reference and the browser should have access to JS file all times?

sample asp source:

<html>
  <body>
    <% response.cookies("tempJs")="yes"%>
    <script src="myscripts.asp"></script>
    <% response.cookies("tempJS")="no"%>
  </body>
</html>

Sample disposable JavaScript (myscripts.asp):

<%
response.ContentType="text/javascript"
if request.cookies("tempJs")="yes" then
%>
    document.write ("Hello world");
<%
end if
%>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My answer is not the answer to the question you have asked but is probably still what you are looking for.

You need to understand the difference between scripts being executed on the server side and the scripts being executed on the client side.
Javascript executes on the client side so anything written between <%%> tags have no effect ones the js has been given in response.

So what you are essentially doing is.

  • Setting a variable yes then
  • Adding the <script></script> tag to your response and then
  • Setting the variable false.
  • After this response is sent back so the value of the variable still is no. The value of the variable is sent back with the response as a Cookie.
  • Now the browser parses the HTML.
  • Sees the tag and requests for myscripts.asp Having the value no set in the cookie which is added to the request.
  • Server sees the value of the cookie as no and renders the script accordingly. Which is your case is without the line document.write ("Hello world");(This is probably the reason you are asking about the caching issues. It is not getting cached.)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...