I observed chunks like below sometimes on web pages. So i am curious to know what this really does? or why its written in such a way?
我有时在网页上看到下面这样的块。我很好奇这到底是怎么回事?或者为什么它是这样写的?
<script src="somefile.js?param1=one¶m2=two" />
< script src = " somefile.js吗?param1 = one¶m2 =两个" / >
i can only make out following few intentions behind it
我只能看出它背后的一些意图
- Its not page URL (i mean .aspx/.php/.jsp etc.) so its not hacking kind of code where user can add code like this to pass data without getting users attention as its tag which does not render on UI OR implementing old type of AJAX alternative
- 它不是页面URL(我的意思是。aspx/。php/。因此,它不会破坏代码,用户可以添加这样的代码来传递数据,而不会引起用户的注意,因为它的标记不会在UI上呈现,也不会实现旧类型的AJAX替代
- This kind of URL param are useful if user do not wish the JS file (any other resource like image) to get cached. This can be quick way to manage caching
- 如果用户不希望JS文件(任何其他资源,比如图像)被缓存,那么这种URL参数是有用的。这是管理缓存的快速方法
But i am unable to figure out following
但是我不知道接下来会发生什么
- Looks like page URL parameters but are these parameters anyway readable in JavaScript file and have some additional utility?
- 看起来像页面URL参数,但是这些参数在JavaScript文件中是可读的吗?
- Do these parameters have any extra role to play here ?
- 这些参数在这里有额外的作用吗?
- What are the other possible practical scenarios where code like this can be/is used?
- 使用这种代码的其他可能的实际场景是什么?
So please provide some inputs related with the same
所以请提供一些与此相关的输入。
Thanks,
谢谢,
6 个解决方案
#1
7
Running Non-JS Code within .js Extensions
In cases like this, that source .js file might (given proper server-configurations) actually have PHP/.NET code within it, which can read those appended values.
在这种情况下,源.js文件(给定适当的服务器配置)实际上可能有PHP/。NET代码,可以读取附加的值。
As you said, Avoiding Cache...
Additionally, people will at times append a random string at the end of their referenced elements to avoid loading cached data.
此外,人们有时会在引用元素的末尾附加一个随机字符串,以避免加载缓存的数据。
#2
4
The URL having '.js' means nothing. It could still be handled by a server-side script like an ASP or PHP.
URL”。js的毫无意义。它仍然可以由服务器端脚本(如ASP或PHP)来处理。
#3
4
Either the javascript file is not static (it is generated by the server based on the parameters in its querystring)
javascript文件不是静态的(它是由服务器根据它的querystring中的参数生成的)
OR
或
In the JavaScript file itself, you can have it check its own querystring parameters (not just that of the page, but that of the javascript source url).
在JavaScript文件本身中,可以让它检查自己的querystring参数(不仅是页面的参数,还包括JavaScript源url的参数)。
OR
或
(This doesn't exactly match your scenario, but) you can also add parameters at the end of image and script urls as a way of versioning. The version with the url="somescript.js?V=3" will be cached by the user until the page then changes and the url is not="somescript.js?V=4". The file will be replaced by the version on the server no matter what the browser setting may be.
(这并不完全符合您的场景,但是)您还可以在图像的末尾添加参数,并将脚本url作为版本控制的一种方式。url="somescript.js? "V=3"将被用户缓存,直到页面发生变化,url不等于"somescript.js?V=4"。无论浏览器设置是什么,该文件都将被服务器上的版本所取代。
My guess (without looking at this specific case) is that the javascript file is reading its own querystring. I have done this, and its very helpful.
我的猜测(不考虑这个特定的情况)是javascript文件正在读取它自己的querystring。我已经这样做了,这很有帮助。
#4
2
Looks like page URL parameters but are these parameters anyway readable in JavaScript file and have some additional utility?
看起来像页面URL参数,但是这些参数在JavaScript文件中是可读的吗?
Yes you can read them in JavaScript, Scriptaculous uses that approach for loading modules, eg:
是的,你可以在JavaScript中阅读它们,Scriptaculous使用这种方式加载模块,例如:
<script type="text/javascript" src="scriptaculous.js?load=effects,dragdrop">
</script>
Do these parameters have any extra role to play here ? What are the other possible practical scenarios where code like this can be/is used?
这些参数在这里有额外的作用吗?使用这种代码的其他可能的实际场景是什么?
That can be also used for server-side script joining and minifying, of course using some url rewriting technique to have the .js extension, and as you say, it's a common technique to add timestamp parameters to break the browser cache.
这也可以用于服务器端脚本的连接和缩小,当然可以使用一些url重写技术来实现.js扩展,正如您所说,添加时间戳参数来破坏浏览器缓存是一种常见的技术。
#5
2
It can be used for three different reasons:
它的使用有三个不同的原因:
1) To generate the JavaScript file in the server depending on the parameters;
1)根据参数在服务器中生成JavaScript文件;
2) To avoid caching;
2)避免缓存;
3) To pass parameters to JavaScript itself
3)将参数传递给JavaScript本身
#6
0
An example of this in practice would be a server side handler for somefile.js that uses the parameters (names of other scripts) to determine which scripts are actually required and combine/minify them, returning them as a single somefile.js script file.
在实践中,这方面的一个例子是某文件的服务器端处理程序。使用参数(其他脚本的名称)来确定哪些脚本实际上是必需的,并将它们合并/缩小,将它们作为单个的somefile返回。js脚本文件。
#1
7
Running Non-JS Code within .js Extensions
In cases like this, that source .js file might (given proper server-configurations) actually have PHP/.NET code within it, which can read those appended values.
在这种情况下,源.js文件(给定适当的服务器配置)实际上可能有PHP/。NET代码,可以读取附加的值。
As you said, Avoiding Cache...
Additionally, people will at times append a random string at the end of their referenced elements to avoid loading cached data.
此外,人们有时会在引用元素的末尾附加一个随机字符串,以避免加载缓存的数据。
#2
4
The URL having '.js' means nothing. It could still be handled by a server-side script like an ASP or PHP.
URL”。js的毫无意义。它仍然可以由服务器端脚本(如ASP或PHP)来处理。
#3
4
Either the javascript file is not static (it is generated by the server based on the parameters in its querystring)
javascript文件不是静态的(它是由服务器根据它的querystring中的参数生成的)
OR
或
In the JavaScript file itself, you can have it check its own querystring parameters (not just that of the page, but that of the javascript source url).
在JavaScript文件本身中,可以让它检查自己的querystring参数(不仅是页面的参数,还包括JavaScript源url的参数)。
OR
或
(This doesn't exactly match your scenario, but) you can also add parameters at the end of image and script urls as a way of versioning. The version with the url="somescript.js?V=3" will be cached by the user until the page then changes and the url is not="somescript.js?V=4". The file will be replaced by the version on the server no matter what the browser setting may be.
(这并不完全符合您的场景,但是)您还可以在图像的末尾添加参数,并将脚本url作为版本控制的一种方式。url="somescript.js? "V=3"将被用户缓存,直到页面发生变化,url不等于"somescript.js?V=4"。无论浏览器设置是什么,该文件都将被服务器上的版本所取代。
My guess (without looking at this specific case) is that the javascript file is reading its own querystring. I have done this, and its very helpful.
我的猜测(不考虑这个特定的情况)是javascript文件正在读取它自己的querystring。我已经这样做了,这很有帮助。
#4
2
Looks like page URL parameters but are these parameters anyway readable in JavaScript file and have some additional utility?
看起来像页面URL参数,但是这些参数在JavaScript文件中是可读的吗?
Yes you can read them in JavaScript, Scriptaculous uses that approach for loading modules, eg:
是的,你可以在JavaScript中阅读它们,Scriptaculous使用这种方式加载模块,例如:
<script type="text/javascript" src="scriptaculous.js?load=effects,dragdrop">
</script>
Do these parameters have any extra role to play here ? What are the other possible practical scenarios where code like this can be/is used?
这些参数在这里有额外的作用吗?使用这种代码的其他可能的实际场景是什么?
That can be also used for server-side script joining and minifying, of course using some url rewriting technique to have the .js extension, and as you say, it's a common technique to add timestamp parameters to break the browser cache.
这也可以用于服务器端脚本的连接和缩小,当然可以使用一些url重写技术来实现.js扩展,正如您所说,添加时间戳参数来破坏浏览器缓存是一种常见的技术。
#5
2
It can be used for three different reasons:
它的使用有三个不同的原因:
1) To generate the JavaScript file in the server depending on the parameters;
1)根据参数在服务器中生成JavaScript文件;
2) To avoid caching;
2)避免缓存;
3) To pass parameters to JavaScript itself
3)将参数传递给JavaScript本身
#6
0
An example of this in practice would be a server side handler for somefile.js that uses the parameters (names of other scripts) to determine which scripts are actually required and combine/minify them, returning them as a single somefile.js script file.
在实践中,这方面的一个例子是某文件的服务器端处理程序。使用参数(其他脚本的名称)来确定哪些脚本实际上是必需的,并将它们合并/缩小,将它们作为单个的somefile返回。js脚本文件。