javascript 文件可以是动态的(每个客户端加载时都不同)吗?如何?
网站上的每个客户端都有自己的 HTML 元素组成。有些可能有 3 个元素,而另一些可能有 123 个。这些元素之间几乎没有相似之处。每个元素都需要特定的代码来操作各个元素。事实上,每个客户在每次搜索或在网站上执行操作后都可能需要不同的 JavaScript 文件。
那么,服务器如何为每个会话动态提供定制的 Javascript 文件呢?
我在服务器端使用 python/cherrypy/mako...
TIA
Dennis
Each client on the site has his own composition of HTML elements. Some may have 3 elements while others may have 123. There is little if any similarity between the elements. And each element requires specific code to manipulate the various elements. In fact each client may need a different JavaScript file after each search or action taken on the site.
So, how can the server supply customized Javascript files on the fly for each session?
I use python/cherrypy/mako on the server side...
TIA
Dennis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您应该认真重新考虑动态生成特定于系统特定用户的 JavaScript 文件的想法。乍一听,这似乎是一个好主意,但它可能指出了一个潜在的设计缺陷,如果纠正这个缺陷,可能会产生一个更简单、更稳定、更容易运行的系统。
鉴于此,生成动态 JavaScript 与生成动态 HTML 几乎没有什么不同。您有一个模板文件,其中包含 JavaScript 的骨架和表示动态部分所在区域的标记标签。然后,您需要有一个可以响应文件请求的 URL 处理程序。通常,JavaScript 文件的处理方式与其他静态文件(例如图像或 CSS)类似,但您可以将其视为动态 HTML 文件的处理程序。
您创建的 HTML 文件必须包含生成的 JS 文件的 URL。比方说,您关闭了一个整数用户 ID。主页的 URL 可能类似于
http://myapp.com/home/11234
,其中 11234 是特定用户的 ID。在该页面的模板内,您必须有一个script
标记,该标记具有指向动态生成的 JavaScript 文件的 URL 的 href,例如http://myapp .com/js/11234
。最后,动态 JavaScript 文件的 URL 必须包含一个 Id 组件,处理程序可以使用该组件来加载使文件特定于给定用户所需的任何数据。
再次强调,我不推荐这种技术。
I think that you should seriously rethink the idea of dynamically-generating JavaScript files that are specific to a particular user of the system. It may sound like a neat idea at first, but it probably points to an underlying design flaw that if corrected could result in a simpler, stabler and more-easily-run system.
Given all that, generating dynamic JavaScript is little or no different than generating dynamic HTML. You have a template file that contains the skeleton of the JavaScript and markup tags that represent the areas where the dynamic parts go. Then, you need to have a URL handler that can respond to requests for the file. Normally, JavaScript files are handled like other static files such as images or CSS, but you'll treat it like a handler for a dynamic HTML file.
The HTML file that you create will have to include the URL for your generated JS file. Let's say, for instance, that you key off of an integer user id. The URL for your main page might be something like
http://myapp.com/home/11234
, where 11234 is the id for the specific user. Inside the template for that page, you'd have to have ascript
tag that has an href that points to the URL for the dynamially-generated JavaScript file, something likehttp://myapp.com/js/11234
.Finally, the URL for the dynamic JavaScript file will have to contain an Id component that the handler can use to load whatever data is required to make the file specific for the given user.
Again, I do not recommend this technique.
使用多个 JavaScript 文件并仅包含您需要的文件。
如果您确实需要动态生成每个 JavaScript 文件,请查看 内联 JavaScript:
Use multiple JavaScript files and just include the ones you need..
If you really do need every JavaScript file to be dynamically generated, take a look at inline JavaScript:
据我了解,生成 HTML 和 JS 文件的过程非常重要,需要同时生成它们。并且发送 HTML 文件后重新生成 JS 文件可能会出现问题。
在这种情况下,您总是可以缓存 JS 文件并为其生成令牌并从 HTML 文件中重新获取它?
这将重新生成对服务器的第二个请求,您可以让 CherryPy(抱歉,不熟悉它)调用一个进程,并简单地返回带有正确 http 标头的 ${token} 的内容...并从中删除该文件磁盘。
这是一个可行的解决方案,而不是重新生成 @Adam Crossland 建议的 JS。
From what I understand, the process of generating the HTML and the JS files is important enough to have them both generated at the same time. And regenerating the JS file after the HTML file is sent could be problematic.
In that case, you can always cache the JS file and generate a token for it and refetch it from the HTML file?
This will regenerate a second request to the server, and you can have CherryPy (sorry, not familiar with it) call a process and simply return the content of the ${token} with the correct http headers... and delete the file from disk.
Is this a viable solution instead of to regenerate the JS suggested by @Adam Crossland.