Vineet
Vineet

Reputation: 5359

How can I use a JavaScript file in my ASP.NET project?

I want to use a JavaScript file, lying in the root folder. How can I include that JS file?

Upvotes: 2

Views: 1317

Answers (4)

alienonland
alienonland

Reputation: 76

If you need to include the JavaScript from the code behind of a page or WebControl, you have a few options

this.Page.ClientScript.RegisterClientScriptInclude(typeof(WebControlThatNeedsIt), "IdentifierOfTheScriptSuchAsLoad", "~/myfile.js");

this.Page.ClientScript.RegisterClientScriptBlock(typeof(WebControlThatNeedsIt), "IdentifierOfTheScriptSuchAsLoad", "<script type=\"text/javascript\" src=\"/myfile.js\"></script>",false);

If not you can use what Quinton Suggested in the markup.

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

Note that using the

</script>

is important, as self closing doesn't work with script tags i.e.:

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

Upvotes: 2

Sebastien Lachance
Sebastien Lachance

Reputation: 2007

Also, if you are using Visual Studio, you can drag the file right into your aspx file and it will generate the correct markup for you.

Upvotes: 0

Jeeva Subburaj
Jeeva Subburaj

Reputation: 1921

use

<script language="javascript" src="FILE PATH"></script>

Upvotes: 3

Quintin Robinson
Quintin Robinson

Reputation: 82325

It should be the same as including it in any standard html page..

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

If this isn't exactly what you are looking for let me know and I'd be happy to expand as far as I can.

Upvotes: 5

Related Questions