How to load multiple javascript files inside HTML page
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 solut开发者_StackOverflow社区ion.
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 ?
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 completion (it's a comment, 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>
精彩评论