一种处理先前使用 $.getScript() 加载的脚本文件的方法
对不起我的英语。我正在开发一个国际象棋互动网站。有一个课程菜单,当用户选择课程时,它会使用 $.getScript()
加载。课程文件只有一个变量 lesson
,其中包含一个“大”json 对象。当用户选择另一节课程时,文件将被加载,并且新的lesson
变量将覆盖前一个课程。
好的,效果很好。但是,我应该处理以前加载的文件吗?如果应该,我该怎么做?如果用户浏览五十个课程,会减慢该过程吗?
当文件加载时,我在 firebug 中查看了 ,据说
$.getScript()
放置了文件,它会发出“黄色闪烁” ',但没有添加任何可见的内容。
我想知道你对此的想法。提前致谢。
Sorry about my English. I'm developing a chess interactive site. There is a lesson menu, when the user selects the lesson, it's loaded using $.getScript()
. Lesson file only has a variable lesson
that contains a "big" json object. When user selects another lesson, file is loaded and the new lesson
var overrides the previous one.
Ok, that works great. But, should I dispose the previous loaded file/s, and if I should, how I do that? What if the user navigates fifty lessons, will it slow down the process?
I took a look in firebug when the file is loaded and the <head>
, where supposedly $.getScript()
put the file, and it makes a 'yellow flash', but nothing visible is added.
I would want to know your thoughts about this. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$.getScript
实际上并不加载文件(即将其作为脚本标记附加到头部),而是下载脚本 .js 文件并立即运行它。当然,您可以在脚本中包含某种终止函数,当您使用完它时,该函数会自行清除。$.getScript
doesn't actually load the file (i.e. appending it to the head as a script tag) but downloads the script .js file and runs it immediately. Of course, you could include some kind of terminating function in your script that wipes itself out when you are done using it.即使您“处置”该文件,或删除脚本标记或其他任何内容,您的脚本也已经被执行,因此您的变量也存在。
唯一的方法是当您选择另一节课程时将 json 对象设置为
null
。例如,如果您的
lesson1.js
文件中有以下 json:在您的主页/脚本中您应该有:
希望这会有所帮助。干杯
Even if you'd 'dispose' the file, or delete the script tag or whatever, your script already has been exeuted, and therefore, your variables exist.
The only way would be to set to
null
your json objects when you select another lesson.For example, if you have the following json in the
lesson1.js
file:In your main page/script you should have:
Hope this helps. Cheers