在哪里放置特定于页面的JavaScript [英] Where to put page-specific JavaScript

查看:64
本文介绍了在哪里放置特定于页面的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小菜单系统,基本上采用HTML格式:


< div id =''foo''>< / div>


并从服务器检索foo.shtml并将其插入

< div>内。但有时我想foo​​.shtml看起来像:


< script language =''JavaScript''>

... do一些AJAX-y

< / script>

< div>< / div>


以便脚本填写页面。我把一些东西砍成了一个

,将foo.shtml插入到foo'的div然后做了一个

fooDiv.getElementsByTagName(''script'')并使用eval()on and it

大部分时间都在一些brossers中工作,但它似乎是hackish和

有点危险,并且它无处不在。肯定有一个

AJAX惯用语(甚至是内置的DOM)来执行脚本,因为

页面的部分加载。有人可以开导我吗? TIA。

解决方案

Christopher Nelson在2007年2月26日上午11:38发表以下内容:


我有一个小菜单系统,基本上采用HTML格式:


< div id =''foo''>< / div>


并从服务器检索foo.shtml并将其插入

< div>内。但有时我想foo​​.shtml看起来像:


< script language =''JavaScript''>

... do一些AJAX-y

< / script>

< div>< / div>


以便脚本填写页面。我把一些东西砍成了一个

,将foo.shtml插入到foo'的div然后做了一个

fooDiv.getElementsByTagName(''script'')并使用eval()on and it

大部分时间都在一些brossers中工作,但它似乎是hackish和

有点危险,并且它无处不在。肯定有一个

AJAX惯用语(甚至是内置的DOM)来执行脚本,因为

页面的部分加载。有人可以开导我吗? TIA。



我对动态加载脚本非常熟悉,所以,这个帖子

可以帮到你。你会找到更多关于这个问题的信息和链接

你可能比想知道的更多:)


< URL:

http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/7e23f42490c301de/3441a1cc21869a10?lnk=gst&q=createTextNode+IE+Randy + Web& rnum = 1& hl = en#3441a1cc21869a10>


-

兰迪

机会有利于准备好的心灵

comp。 lang.javascript常见问题 - http://jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


2月26日下午1:01,Randy Webb< HikksNotAtH ... @ aol.comwrote:


Christopher Nelson在2007年2月26日上午11:38发表以下内容:


我有一个小菜单系统,它基本上采用HTML:


< div id =''foo''> ;< / DIV>


并从服务器检索foo.shtml并将其插入

< div>内。但有时候我希望foo.shtml看起来像:


< script language =''JavaScript''>

......做点什么AJAX-y

< / script>

< div>< / div>


以便脚本填写页面。我把一些东西砍成了一个

,将foo.shtml插入到foo'的div然后做了一个

fooDiv.getElementsByTagName(''script'')并使用eval()on and it

大部分时间都在一些brossers中工作,但它似乎是hackish和

有点危险,并且它无处不在。肯定有一个

AJAX惯用语(甚至是内置的DOM)来执行脚本,因为

页面的部分加载。有人可以开导我吗? TIA。



我对动态加载脚本非常熟悉,所以,这个帖子

可以帮到你。你会找到关于这个问题的更多信息和链接

你可能想要知道的东西。


< URL:http:// groups。 google.com.au/group/comp.lang.javascript/browse_frm/thr...>



谢谢。我希望这个帖子结束,这是最好的方式来做它b $ b ... :-(似乎最便携的获取脚本的方法是加载脚本元素并设置scr属性。
http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/ 我发现:

函数cElement(fileName){

var s = document.createElement(''script'');

s.src = fileName; // JS文件的名称。

document.getElementById(''scriptDiv'')。appendChild(s);

}


大多数相应的列都是绿色的。这是一个准确的

摘要吗?对于我的情况,似乎不是有foo.shtml

带有嵌入式脚本标记,我应该有一些带有一些

结构的foo.shtml和一个伴随foo.js,它们被加载到那个结构中

上面的代码。或者我错过了这条船?


2月26日下午2:50,Christopher Nelson < cnel ... @ nycap.rr.comwrote:


2月26日下午1:01,Randy Webb< HikksNotAtH ... @ aol.comwrote :


Christopher Nelson在2007年2月26日上午11:38发表以下内容:

< blockquote class =post_quotes>
我有一个小菜单系统,它基本上采用HTML:


< div id =''foo''>< / div>


并从服务器检索foo.shtml并将其插入

< div>。但有时候我希望foo.shtml看起来像:


< script language ='''JavaScript''>

...做一些事情AJAX-y

< / script>

< div>< ; / DIV>


以便脚本填写页面。我把一些东西砍成了一个

,将foo.shtml插入到foo'的div然后做了一个

fooDiv.getElementsByTagName(''script'')并使用eval()on and it

大部分时间都在一些brossers中工作,但它似乎是hackish和

有点危险,并且它无处不在。肯定有一个

AJAX惯用语(甚至是内置的DOM)来执行脚本,因为

页面的部分加载。有人可以开导我吗? TIA。


我对动态加载脚本非常熟悉,所以,这个帖子

可以帮到你。你会找到关于这个问题的更多信息和链接

你可能想要知道:)


<网址:HTTP://groups.google.com.au/group/comp.lang.javascript/browse_frm/thr...>



谢谢。我希望这个帖子结束,这是最好的方式来做它b $ b ... :-(似乎最便携的获取脚本的方法是加载脚本元素并设置scr属性.Athttp://members.aol.com/_ht_a/hikksnotathome/loadJSFile/I found :


函数cElement(fileName){

var s = document.createElement(''script'');

s .src = fileName; // JS文件的名称。

document.getElementById(''scriptDiv'')。appendChild(s);

}


大部分相应的列都是绿色的。这是一个准确的

摘要吗?对于我的情况,似乎不是有foo.shtml
$ b带有嵌入式脚本标签的$ b,我应该有一些带有一些

结构的foo.shtml和一个伴随foo.js,它们被加载到该结构中

,上面的代码。或者我错过了船吗?



迈克尔福斯特认为这是要走的路:

http://www.cross-browser.com/x/lib/v...ym=xLoadScript


I have a little menu system which essentially takes HTML like:

<div id=''foo''></div>

and retrieves foo.shtml from the server and inserts it inside the
<div>. But sometimes I''d like foo.shtml to look like:

<script language=''JavaScript''>
...do something AJAX-y
</script>
<div></div>

so that the script fills in the page. I''ve hacked together something
that inserts the foo.shtml into foo''s div then does a
fooDiv.getElementsByTagName(''script'') and uses eval() on them and it
works most of the time in some brossers but it seems hackish and
somewhat dangerous and it doesn''t work everywhere. Surely there''s an
AJAX idiom (or even a DOM built-in) to execute scripts as parts of
pages load. Can someone enlighten me? TIA.

解决方案

Christopher Nelson said the following on 2/26/2007 11:38 AM:

I have a little menu system which essentially takes HTML like:

<div id=''foo''></div>

and retrieves foo.shtml from the server and inserts it inside the
<div>. But sometimes I''d like foo.shtml to look like:

<script language=''JavaScript''>
...do something AJAX-y
</script>
<div></div>

so that the script fills in the page. I''ve hacked together something
that inserts the foo.shtml into foo''s div then does a
fooDiv.getElementsByTagName(''script'') and uses eval() on them and it
works most of the time in some brossers but it seems hackish and
somewhat dangerous and it doesn''t work everywhere. Surely there''s an
AJAX idiom (or even a DOM built-in) to execute scripts as parts of
pages load. Can someone enlighten me? TIA.

I am vaguely familiar with dynamically loading scripts, so, this thread
may help you. You will find more information and links about the problem
you have than you probably wanted to know :)

<URL:
http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/7e23f42490c301de/3441a1cc21869a10?lnk=gst&q=createTextNode+IE+Randy +Web&rnum=1&hl=en#3441a1cc21869a10>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


On Feb 26, 1:01 pm, Randy Webb <HikksNotAtH...@aol.comwrote:

Christopher Nelson said the following on 2/26/2007 11:38 AM:

I have a little menu system which essentially takes HTML like:

<div id=''foo''></div>

and retrieves foo.shtml from the server and inserts it inside the
<div>. But sometimes I''d like foo.shtml to look like:

<script language=''JavaScript''>
...do something AJAX-y
</script>
<div></div>

so that the script fills in the page. I''ve hacked together something
that inserts the foo.shtml into foo''s div then does a
fooDiv.getElementsByTagName(''script'') and uses eval() on them and it
works most of the time in some brossers but it seems hackish and
somewhat dangerous and it doesn''t work everywhere. Surely there''s an
AJAX idiom (or even a DOM built-in) to execute scripts as parts of
pages load. Can someone enlighten me? TIA.


I am vaguely familiar with dynamically loading scripts, so, this thread
may help you. You will find more information and links about the problem
you have than you probably wanted to know :)

<URL:http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thr...>

Thanks. I wish that thread ended, "And this is the best way to do
it..." :-( It seems that the most portable method of getting scripts
loaded is creating script elements and setting the scr property. At
http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/ I found:

function cElement(fileName) {
var s = document.createElement(''script'');
s.src = fileName; //the name of the JS file.
document.getElementById(''scriptDiv'').appendChild(s );
}

With most of the corresponding column green. Is that an accurate
summary? For my situation, it seems that rather than having foo.shtml
with an embedded script tag, I should have foo.shtml with some
structure and a companion foo.js which gets loaded into that structure
with the code above. Or am I missing the boat somehow?


On Feb 26, 2:50 pm, "Christopher Nelson" <cnel...@nycap.rr.comwrote:

On Feb 26, 1:01 pm, Randy Webb <HikksNotAtH...@aol.comwrote:

Christopher Nelson said the following on 2/26/2007 11:38 AM:

I have a little menu system which essentially takes HTML like:

<div id=''foo''></div>

and retrieves foo.shtml from the server and inserts it inside the
<div>. But sometimes I''d like foo.shtml to look like:

<script language=''JavaScript''>
...do something AJAX-y
</script>
<div></div>

so that the script fills in the page. I''ve hacked together something
that inserts the foo.shtml into foo''s div then does a
fooDiv.getElementsByTagName(''script'') and uses eval() on them and it
works most of the time in some brossers but it seems hackish and
somewhat dangerous and it doesn''t work everywhere. Surely there''s an
AJAX idiom (or even a DOM built-in) to execute scripts as parts of
pages load. Can someone enlighten me? TIA.

I am vaguely familiar with dynamically loading scripts, so, this thread
may help you. You will find more information and links about the problem
you have than you probably wanted to know :)

<URL:http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thr...>


Thanks. I wish that thread ended, "And this is the best way to do
it..." :-( It seems that the most portable method of getting scripts
loaded is creating script elements and setting the scr property. Athttp://members.aol.com/_ht_a/hikksnotathome/loadJSFile/I found:

function cElement(fileName) {
var s = document.createElement(''script'');
s.src = fileName; //the name of the JS file.
document.getElementById(''scriptDiv'').appendChild(s );
}

With most of the corresponding column green. Is that an accurate
summary? For my situation, it seems that rather than having foo.shtml
with an embedded script tag, I should have foo.shtml with some
structure and a companion foo.js which gets loaded into that structure
with the code above. Or am I missing the boat somehow?

It seems Michael Foster thinks this is the way to go:

http://www.cross-browser.com/x/lib/v...ym=xLoadScript


这篇关于在哪里放置特定于页面的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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