1

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 ?

3
  • How do you see test11.js and test12.js are actually loaded? Commented Jun 16, 2011 at 11:35
  • @marcel because i get exception of js function undefined on page load. Commented Jun 16, 2011 at 11:50
  • In that case, they are not loaded. Commented Jun 17, 2011 at 9:27

3 Answers 3

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)
};
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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>

2 Comments

So if i am using same set of scripts set1 in 5-6 pages, then i will have to change in all 5-6 pages whenever i need to add or remove a js file. isnt there an indirection solution so that i make change only in 1 file (like set1.js) for easier maintenance. If there isnt any solution then i will go ahead with waht you siggested.
That's what the masterpage is for - create a masterpage and make all your aspx pages use the masterpage, then you have a single point of change. Google asp.net masterpage for examples of how to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.