如何从html代码外包Javascript代码? [英] How to outsource Javascript code from html code ?

查看:83
本文介绍了如何从html代码外包Javascript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从正常提取Javascript代码? HTML代码

(例如类似于可以放入单独文件的CSS代码)?

如果您提供解决方案:我可以在您的解决方案中确定应该在主HTML代码中放置/插入代码的哪个部分




有时Javascript功能被分成几个部分和/或者

它会对Javascript代码运行/调用产生很大影响

(例如

- 在< HEAD> .. ..< / HEAD>或

- 在< BODY>标记后面的开头

- 在< / BODY>标记之前的末尾。

.....)


Arty

解决方案

< blockquote>在文章< bs ************* @ news.t-online.com> ;, ar ** *@redseven.com

说...

有没有办法从正常中提取Javascript代码? HTML代码
(例如类似于可以放入单独文件的CSS代码)?

如果您提供解决方案:我可以在您的解决方案中确定哪个部分和哪个部分
应该在主HTML代码中放置/插入代码吗?

有时Javascript功能被分成几个部分和/或它们在Javascript代码运行的地方有很大的不同/被称为
(例如
- 在< HEAD> ....< / HEAD>或
- 在< BODY>标签后面的开头
- 在< / BODY>标记之前的末尾。





-

Hywel我不吃乳蛋饼
http://hyweljenkins.co.uk/
http://hyweljenkins.co.uk/mfaq.php

Arthur Connor写道:

有没有办法从正常中提取Javascript代码? HTML代码
(例如类似于可以放入单独文件的CSS代码)?


我不确定你的意思是提取。


有一个书签可以让你查看给定HTML页面中的所有

javascript代码。


您可以将所有脚本函数和代码放入单个外部

javascript文件中。

如果您提供解决方案:我可以在您的解决方案中确定哪个部分和哪个部分
代码应该放在/插入主HTML代码吗?


再次,你的问题有点模糊。


如果你使用一个自定义的书签,然后是的,你可以找出来自外部文件的内联

脚本和脚本。


可以找出给定的书签在哪里调用函数的HTML文档。


有时Javascript功能被分成几个部分和/或
它对Ja有很大的不同vascript代码应该运行/被称为
(例如
- 在< HEAD> ....< / HEAD>或者
- 在< BODY>后面的开头标签
- 在< / BODY>之前的最后标签。
....

Arty




这是因为某些预期的效果或因为某些功能应该

在其他人之前执行(比如翻转图像)或因为

在页面加载时应执行某些功能(例如:

document.write()调用)或者因为一些其他函数应该只有在页面最终完全加载后才执行
(使用

defer属性或使用onload属性身体元素或

窗口对象)等等......这一切都取决于整个代码,目标

效果/结果等等。没有单身这里有独特的规则。

总的来说,最好将javascript代码模块化为定义的函数

,最好远离document.write()调用。 />
一般情况下,我认为最好创建一个唯一的外部

javascript文件,以便

- 您的代码可以重复使用
- 您的代码可以在同一个会话中缓存和重用,减少http

下载和再次解析,使脚本功能更加响应

响应,snappy 。


DU


" Arthur Connor" < AR *** @ redseven.com>在留言中写道

新闻:bs ************* @ news.t-online.com ...

有没有办法从正常提取Javascript代码HTML
代码(例如类似于可以放入单独文件的CSS代码)?

如果您提供解决方案:我可以在您的解决方案中确定哪个和哪个
部分应该在主HTML代码中放置/插入代码吗?

有时Javascript功能被分成几个部分和/或它们在Javascript代码运行的地方有很大的不同/被称为
(例如
- 在< HEAD> ....< / HEAD>或
- 在< BODY>标签后面的开头
- 最后在< / BODY>标签之前。

Arty



我想这取决于什么你的确切用法是。在我的网站上,我有两个

不同的js应用程序。一个处理图像交换和弹出显示

并且取决于在使用它们之前定义的功能所以我

在< head>结束之前包含了定义;如
所示,这个废料:

....

< title>

Bodger的新日

< / title>

<! - 插入图像例程 - >

< script type =" text / javascript" ; src =" theimager.js">

< / script>

<! - 插入结束 - >

< / head>

....


当读者点击按钮图像并且

看起来像这样:


....

< a href =

" javascript < b> :newWindow = openWin('gallery / gallery-01-small.jpg',

600,424); newWindow.focus()">

< img src =" button / small.gif"类= QUOT; gbutt" ALT =""标题= QUOT;" width =" 30"

height =" 20" />< / a>

....


其中openWin()是先前定义的并且使用所需的
$来调用b $ b参数在这里创建一个新窗口,其中包含图像和焦点()

将新窗口弹出父窗口。


In第二个主要应用程序没有功能定义所以没有什么

需要出现在< head>所以代码放在需要的地方

内联如下:


....

< ;! - TheCounter.com代码的开始 - >

< script type =" text / javascript" src =" thecounter.js">

< / script>

<! - TheCounter.com代码结束 - >

....


这样做是因为thecounter.com提供的js非常难看而且

完全消除了任何验证尝试。


这就是你要找的东西吗?

-

John McGaw

[美国田纳西州诺克斯维尔]


退货地址不起作用。请

在小组或通过我的网站回复:
http:// johnmcgaw .com


Is there a way of extracting the Javascript code from the "normal" HTML code
(e.g. similar to CSS code which can be put into a separate file) ?

If you offer a solution: can I determine in your solution where and which part
of the code should be placed/inserted in the main HTML code?

Sometimes a Javascript functionality is split up to several parts and/or
it makes a big difference where the Javascript code should run/be called
(e.g.
- in the <HEAD>....</HEAD> or
- at the beginning just behind the <BODY> tag
- at the end just before the </BODY> tag.
.....)

Arty

解决方案

In article <bs*************@news.t-online.com>, ar***@redseven.com
says...

Is there a way of extracting the Javascript code from the "normal" HTML code
(e.g. similar to CSS code which can be put into a separate file) ?

If you offer a solution: can I determine in your solution where and which part
of the code should be placed/inserted in the main HTML code?

Sometimes a Javascript functionality is split up to several parts and/or
it makes a big difference where the Javascript code should run/be called
(e.g.
- in the <HEAD>....</HEAD> or
- at the beginning just behind the <BODY> tag
- at the end just before the </BODY> tag.



<script src="myfile.js" type="text/javascript"></script>

--
Hywel I do not eat quiche
http://hyweljenkins.co.uk/
http://hyweljenkins.co.uk/mfaq.php


Arthur Connor wrote:

Is there a way of extracting the Javascript code from the "normal" HTML code
(e.g. similar to CSS code which can be put into a separate file) ?

I''m not sure what you mean with "extracting".

There is a bookmarklet available which will allow you to view all
javascript code in a given HTML page.

You can put all script functions and code into a single external
javascript file.
If you offer a solution: can I determine in your solution where and which part
of the code should be placed/inserted in the main HTML code?

Again, your question is a bit fuzzy.

If you use a customized bookmarklet, then yes you can figure out inline
scripts and scripts coming from an external file.

One can figure out where in a given HTML document the functions are called.

Sometimes a Javascript functionality is split up to several parts and/or
it makes a big difference where the Javascript code should run/be called
(e.g.
- in the <HEAD>....</HEAD> or
- at the beginning just behind the <BODY> tag
- at the end just before the </BODY> tag.
....)

Arty



That''s because of some desired effects or because some functions should
be executed before some others (like for rollover images) or because
some functions should be executed while the page is loading (e.g.:
document.write() calls) or because some other functions should be
executed only once the page has finally and completely loaded (with the
defer attribute or with the onload attribute of the body element or
window object), etc... It all depends on the whole code, the targeted
effects/results, etc.. There is no single and unique rule here.
Overall, it is best to modularize javascript code into defined functions
and it is best to stay away from document.write() calls.
In general, I think it is best to create a single unique external
javascript file so that
- your code can be reused
- your code can be cached and reused in the same session, reducing http
downloads and parsing again, making script functions a bit more
responsive, snappy.

DU


"Arthur Connor" <ar***@redseven.com> wrote in message
news:bs*************@news.t-online.com...

Is there a way of extracting the Javascript code from the "normal" HTML code (e.g. similar to CSS code which can be put into a separate file) ?

If you offer a solution: can I determine in your solution where and which part of the code should be placed/inserted in the main HTML code?

Sometimes a Javascript functionality is split up to several parts and/or
it makes a big difference where the Javascript code should run/be called
(e.g.
- in the <HEAD>....</HEAD> or
- at the beginning just behind the <BODY> tag
- at the end just before the </BODY> tag.
....)

Arty


I guess it depends on what your exact usage is. At my site I have two
different applications for js. One handles image swapping and popup display
and depends on having functions defined in advance of them being used so I
have the definitions included right before the end of the <head> as shown in
this scrap:
....
<title>
Bodger's New Day
</title>
<!-- insert image routines -->
<script type="text/javascript" src="theimager.js">
</script>
<!-- end of insert -->
</head>
....

And the actual call is made when a reader clicks on an image of a button and
looks like this:

....
<a href=
"javascript: newWindow = openWin( 'gallery/gallery-01-small.jpg',
600, 424 ); newWindow.focus()">
<img src="button/small.gif" class="gbutt" alt="" title="" width="30"
height="20" /></a>
....

where openWin() is defined earlier and is called with the required
parameters here to create a new window with an image in it and the focus()
pops the new window up over the parent.

In the second main application there is no function definition so nothing
needs to appear in the <head> so the code is placed where it is needed
inline like this:

....
<!-- Start of TheCounter.com Code -->
<script type="text/javascript" src="thecounter.js">
</script>
<!-- End of TheCounter.com Code -->
....

This is done because the js provided by thecounter.com is pretty ugly and
totally blows away any attempts at validation.

Is this what you were looking for?
--
John McGaw
[Knoxville, TN, USA]

Return address will not work. Please
reply in group or through my website:
http://johnmcgaw.com


这篇关于如何从html代码外包Javascript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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