浏览器可以处理一次性JS文件吗? [英] Can browser handle a disposable JS file?

查看:147
本文介绍了浏览器可以处理一次性JS文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从直接访问中隐藏JS的源.所以我想制作一个一次性的JS文件.我使用一个临时会话cookie.该cookie是在包含JS文件之前设置的,然后过期.

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.

我的确切问题不是浏览器本身,而是浏览器如何处理JS文件?

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?

示例 asp 来源:

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

示例一次性JavaScript(myscripts.asp):

Sample disposable JavaScript (myscripts.asp):

<%
response.ContentType="text/javascript"
if request.cookies("tempJs")="yes" then
%>
    document.write ("Hello world");
<%
end if
%>

推荐答案

我的答案不是您所提出问题的答案,但可能仍然是您要寻找的东西.

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

您需要了解在服务器端执行的脚本与在客户端端执行的脚本之间的区别.
Javascript在客户端执行,因此在<%%>标记之间编写的任何内容都不会对js作出响应产生任何影响.

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.

  • 然后先设置变量yes
  • 在响应中添加<script></script>标签,然后
  • 将变量设置为false.
  • 此响应发送回后,因此变量的值仍为no.变量的值以Cookie的形式发送回去.
  • 现在浏览器解析HTML.
  • 查看标签,并请求myscripts.asp在添加到请求中的cookie中没有设置值no.
  • 服务器将cookie的值视为no,并相应地呈现脚本.您的情况是没有document.write ("Hello world");行(这可能是您询问缓存问题的原因.它没有被缓存.)
  • 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.)

这篇关于浏览器可以处理一次性JS文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆