有很多不同的方法可以在html页面中包含JavaScript.我知道以下选项:
不计算来自硬盘,javascript:URIs和onEvent
-attributes [ 3 ]的浏览器脚本,已经有16个替代方法可以执行JS,我确信我忘了一些东西.
我不太关心快速(并行)加载,我对执行顺序更感兴趣(这可能取决于加载顺序和文档顺序).是否有一个很好的(跨浏览器)参考,涵盖所有情况?例如http://www.websiteoptimization.com/speed/tweak/defer/仅处理其中的6个,并且主要测试旧浏览器.
我担心没有,这是我的具体问题:我有一些(外部)头脚本用于初始化和脚本加载.然后我在身体的末尾有两个静态的内联脚本.第一个允许脚本加载器动态地将另一个脚本元素(引用外部js)附加到正文.第二个静态内联脚本想要使用添加的外部脚本中的js.它可以依赖于已执行的其他(以及为什么:-)?
在我的页面体中,我需要插入此代码作为AJAX调用的结果:
<p>Loading jQuery</p>
<script type='text/javascript' src='scripts/jquery/core/jquery-1.4.4.js'></script>
<p>Using jQuery</p>
<script type='text/javascript'>
$.ajax({
...
});
</script>
Run Code Online (Sandbox Code Playgroud)
$.load()
由于文档已经加载,我无法使用,因此事件不会触发.
这样安全吗?如果没有,如何在执行自定义生成的代码之前确保已加载jquery脚本.
我想我会想出一个灵活的方法来扩展Rails 3.x gem中的ApplicationController.
在我的宝石中lib/my_namespace/my_controller.rb
,我有:
class MyNamespace::MyController < ApplicationController
before_filter :some_method
after_filter :another_method
def initialize
# getting classname of the subclass to use for lookup of the associated model, etc.
# and storing the model_class in an instance variable
# ...
end
# define :some_method, :another_method, etc.
# ...
private
attr_accessor :subclass_defined_during_initialize # etc.
# etc.
end
Run Code Online (Sandbox Code Playgroud)
但是当加载Gem时,app/controllers/application_controller.rb
尚未加载,因此失败:
/path/to/rvm/gemset/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:
in `require': cannot load such file -- my_gem_name/application_controller (LoadError)
Run Code Online (Sandbox Code Playgroud)
作为一种解决方法,我在我的gem中定义了ApplicationController lib/gem_namespace/application_controller.rb
:
class ApplicationController < ActionController::Base
end
Run Code Online (Sandbox Code Playgroud)
我假设即使我在那里定义它,它也会在我的Rails …
gem ruby-on-rails applicationcontroller ruby-on-rails-3 load-order
我有一个打字稿应用程序,动态添加指向JS文件的脚本标记.由于一些限制,我不能在html文件中静态定义这些脚本标记,所以我通过typescript动态添加它们,如下所示:
for (let src of jsFiles) {
let sTag = document.createElement('script');
sTag.type = 'text/javascript';
sTag.src = src;
sTag.defer = true;
document.body.appendChild(script);
}
Run Code Online (Sandbox Code Playgroud)
现在,我注意到,当我动态添加脚本标签时,它们似乎不是按照加载顺序的保证.不幸的是,该jsFiles
阵列具有相互依赖的脚本.因此,数组中的第二个脚本只能在第一个完全加载后加载.第二个脚本引用第一个脚本中定义的函数.有没有一种方法可以指定脚本在动态添加脚本时的顺序和执行顺序(类似于在html文件中静态定义脚本标记时的排序方式)?
PS我想避免使用onload回调来解决这个问题,因为我注意到我的应用程序性能下降.我的第二个脚本文件非常大,我假设这导致了降级.