admin管理员组

文章数量:1334381

I have many js files (say divided into set1 and set2), which are reused in multiple HTML and aspx pages. Using VS 2010.

I tried in head of the page1.html

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

set1.js has:

/// < reference path="test11.js" />
/// < reference path="test12.js" />

All js files are in the Scripts folder of the VS solution.

Will the page1.html load both test11.js and test12.js . It seems to be not happening and i get function (javascript function which i use on button events etc) undefined error while page loads.

What is the correct way to do it ? and what is the reference tag for ?

I have many js files (say divided into set1 and set2), which are reused in multiple HTML and aspx pages. Using VS 2010.

I tried in head of the page1.html

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

set1.js has:

/// < reference path="test11.js" />
/// < reference path="test12.js" />

All js files are in the Scripts folder of the VS solution.

Will the page1.html load both test11.js and test12.js . It seems to be not happening and i get function (javascript function which i use on button events etc) undefined error while page loads.

What is the correct way to do it ? and what is the reference tag for ?

Share Improve this question edited Jun 16, 2011 at 11:32 Munish Goyal asked Jun 16, 2011 at 11:28 Munish GoyalMunish Goyal 1,4094 gold badges28 silver badges49 bronze badges 3
  • How do you see test11.js and test12.js are actually loaded? – Marcel Korpel Commented Jun 16, 2011 at 11:35
  • @marcel because i get exception of js function undefined on page load. – Munish Goyal Commented Jun 16, 2011 at 11:50
  • In that case, they are not loaded. – Marcel Korpel Commented Jun 17, 2011 at 9:27
Add a ment  | 

3 Answers 3

Reset to default 2

you have to include the scripts by using the <script> tag in the html <head>

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

if you want to dynamically do this using javascript and jquery you could use the following example presuming you have an aray of javascript files you want to include:

example1 using jquery and javascript:

for (var i = 0; i < scripts.length; i++)
{ 
    $head.append("<script src=\"" + scripts[i] + "\" type=\"text/javascript\"></script>")
};

example2: using only javascript without jquery:

for (var i = 0; i < scripts.length; i++)
{ 
    var scriptElem=document.createElement('script')
    scriptElem.setAttribute("type","text/javascript")
    scriptElem.setAttribute("src", scripts[i])
    document.getElementsByTagName("head")[0].appendChild(scriptElem)
};

The reference tag is only for Intellisense pletion (it's a ment, so JS ignores these lines).

You have to use the <script> tag for each script file you want to include.

Put all your script references in the master-page head section - much easier to manage

<head>
  <script type="text/javascript" src="x1.js"></script>
  <script type="text/javascript" src="x2.js"></script>
  ...
</head>

本文标签: visual studioHow to load multiple javascript files inside HTML pageStack Overflow