更好的方式动态链接到外部js文件? [英] better way to dynamically link to an external js file?

查看:55
本文介绍了更好的方式动态链接到外部js文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在使用


document.write("< script language =''javascript''src =''jsFile" + i +

" .js''>< / script>");


它有效 - 我在每个文件中都有很多数据,只想要

访客必须下载必要的文件。是否有更好的方法

这样做?


好​​奇,因为页面没有在HTML验证器中验证。

Right now I''m using

document.write("<script language=''javascript'' src=''jsFile" + i +
".js''></script>");

It works -- I have a lot of data in each file and only want the
visitor to have to download the necessary file. Is there a better way
to do this?

Just curious because the page doesn''t validate in HTML validators.

推荐答案

Nick写道:
Nick wrote:
document.write("< script language =''javascript''src =''jsFile" ; + i +
" .js''>< / script>");


....

页面在HTML验证器中无法验证。
document.write("<script language=''javascript'' src=''jsFile" + i +
".js''></script>");
....
the page doesn''t validate in HTML validators.



http://validator.w3.org/docs/help.html#faq -javascript


-

David Dorward< http://blog.dorward.me.uk/> < http://dorward.me.uk/>

Home是〜/ .bashrc的地方



http://validator.w3.org/docs/help.html#faq-javascript

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is


vc ******* @ spamcop.net (尼克)在留言中写道:< 37 ****** ********************@posting.google。 com> ...
vc*******@spamcop.net (Nick) wrote in message news:<37**************************@posting.google. com>...
现在我正在使用

document.write("< script language =''javascript''src =''jsFile" + i +
" .js''>< / script>");

它有效 - 我在每个文件中都有很多数据,只想要<访问者必须下载必要的文件。有没有更好的方法来做到这一点?

好奇,因为页面没有在HTML验证器中验证。
Right now I''m using

document.write("<script language=''javascript'' src=''jsFile" + i +
".js''></script>");

It works -- I have a lot of data in each file and only want the
visitor to have to download the necessary file. Is there a better way
to do this?

Just curious because the page doesn''t validate in HTML validators.




是的,我有同样的问题,我决定使用

document.createElement,以便你如何做到这一点。


var newScript = document.createElement(" script");

newScript.setAttribute(" language"," javascript");

newScript.setAttribute(" src" ,jsFile+ + i +。js;


现在,根据您使用的浏览器,某些选项是

avaliable here。

对于mozilla你可以做

document.body.appendChild(newScript);


否则你可以这样做

document.documentElement.appendChild(newScript);


但我决定做的是

document.getElementById(" scriptBase")。 appendChild(newScript);


如果你有很多o这些脚本就像你说你可以制作一个数组,

for循环和测试人员看看它是否是正确的客户端



Yes I had the same problem and I decided to use the
document.createElement so as for an example of how you would do this.

var newScript = document.createElement("script");
newScript.setAttribute("language","javascript");
newScript.setAttribute("src","jsFile" + i + ".js");

Now depending on what browser you are useing some options are
avaliable here.
For mozilla you can do
document.body.appendChild(newScript);

otherwise you can do this
document.documentElement.appendChild(newScript);

but what I decided to do is
document.getElementById("scriptBase").appendChild( newScript);

If you have alot of these scripts like you say you can make an array,
for loop and a tester to see if its the right client


6月15日2004 18:13:52 -0700,Nick< vc ******* @ spamcop.net>写道:
On 15 Jun 2004 18:13:52 -0700, Nick <vc*******@spamcop.net> wrote:
现在我正在使用

document.write("< script language =''javascript''src =''jsFile" ; + i +
" .js''>< / script>");

它有效 - 我在每个文件中都有很多数据,只想要访问者必须下载必要的文件。有没有更好的方法来做到这一点?

好奇,因为页面没有在HTML验证器中验证。
Right now I''m using

document.write("<script language=''javascript'' src=''jsFile" + i +
".js''></script>");

It works -- I have a lot of data in each file and only want the
visitor to have to download the necessary file. Is there a better way
to do this?

Just curious because the page doesn''t validate in HTML validators.




尼克,

如果javascript有''<''或''&'',

,那么xhtml将无效其他一些细节 - 请查看David发布的链接

(这会引导您: http://www.htmlhelp.com/tools/validator/problems.html)

另请查看xhtml规范。


这是另一个想法 - 一个非常跨浏览器和

也将允许你的xhtml文件验证:-)


*在你的xhtml文件中...


< script type =''text / javascript''>

var jsFileNumber = 1;

< / script>

< script type =''text / javascript''src =''loader.js''>< / script>

*在loader.js中......


document.write("<" +" script type =''text / javascript''src =''jsFile" + jsFileNumber +" .js''><" +" / script>");



Hi Nick,

The xhtml will be invalid if the javascript has ''<'' or ''&'',
and there are a few other details - check out the link David posted
(which will lead you to: http://www.htmlhelp.com/tools/validator/problems.html).
Also check out the xhtml spec.

Here''s another idea - one which is very cross-browser and
will also allow your xhtml file to validate :-)

* In your xhtml file...

<script type=''text/javascript''>
var jsFileNumber = 1;
</script>
<script type=''text/javascript'' src=''loader.js''></script>
* In loader.js...

document.write("<" + "script type=''text/javascript'' src=''jsFile" + jsFileNumber + ".js''><" + "/script>");


这篇关于更好的方式动态链接到外部js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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