一般来说有两种方式。写在界面上和使用.js文件。
1.1界面上的Head部分
能够直接放在head标签内,例如以下代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>testPage</title>
    <script type="text/javascript">
     //your js code
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

1.2界面上的body部分

一般都是直接放在body部分的,例如以下

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>testPage</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
        <script type="text/javascript">
            //your js code
    </script>
</body>
</html>

放在head和body中没有什么差别,一般代码量不多的时候,并且仅仅有当前页面使用这些js。那就直接写在界面上吧。

2、JS文件

对于那些复杂的,并且代码量非常多的JS。最好放在专门的一个.js文件中。然后在页面上依照js文件的相对路径引用进来。

这种优点是,能够防止非常多反复的js代码。能够将一些公用的js方法放在外部js文件中。

比方。一般使用visual studio 2010新建的asp.netproject中都带的有jquery-1.4.1.js文件,我们看怎么使用这个js文件。

比方页面的文件结构如图,

javascript广告 js广告代码放在哪个位置_javascript



要在MyJSFrm.aspx中使用这个js文件就这样引入。


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>testPage</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
    <script type="text/javascript" src="/Scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript">
            //your js code
    </script>
</body>
</html>

总之。别忘了使用相对文件夹,假设当前页面文件的文件夹层级比較深,那就使用../自己算文件夹的层级吧。