Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include Javascript file in Asp.Net page

I want to do some client side validation using javascript in ASP.NET page.

I tried using

<script src="../../../JS/Registration.js" language="javascript" type="text/javascript" />

but its not working. Please help.

like image 457
Vaibhav Jain Avatar asked Nov 03 '09 11:11

Vaibhav Jain


People also ask

Where do I put JavaScript in ASPX page?

In a master page the javascript functionality must be there in head section under script tag. In the content pages, The javascript code can be in content tag which has contentplaceholderid as head which is also there in master page as contentplaceholder in head section of master page.

How do I include a JavaScript file in a Web page?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

HOW include js file in ASP.NET MVC?

The recommended approach is to put in a separate JavaScript file or inside a section defined in Layout page. A section can be added in the MVC Layout page using @RenderSection() directive. For example, we can define a section in Layout page under <head> tag for scripts like below.

Which method enables you to add a reference to a JavaScript file embedded in an assembly?

To embed a client script file in an assembly. In Visual Studio, create a new class library project named SampleControl. Add references to the System. Web, System.


1 Answers

If your page is deeply pathed or might move around and your JS script is at "~/JS/Registration.js" of your web folder, you can try the following:

<script src='<%=ResolveClientUrl("~/JS/Registration.js") %>'  type="text/javascript"></script> 
like image 129
o.k.w Avatar answered Sep 22 '22 04:09

o.k.w