28

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.

rick schott
  • 21,012
  • 5
  • 52
  • 81
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163

6 Answers6

49

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>
o.k.w
  • 25,490
  • 6
  • 66
  • 63
24

add like

<head runat="server">
<script src="Registration.js" type="text/javascript"></script>
</head>

OR can add in code behind.

Page.ClientScript.RegisterClientScriptInclude("Registration", ResolveUrl("~/js/Registration.js"));
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
17

Probably the file is not in the path specified. '../../../' will move 3 step up to the directory in which the page is located and look for the js file in a folder named JS.

Also the language attribute is Deprecated.

See Scripts:

18.2.1 The SCRIPT element

language = cdata [CI]

Deprecated. This attribute specifies the scripting language of the contents of this element. Its value is an identifier for the language, but since these identifiers are not standard, this attribute has been deprecated in favor of type.

Edit

Try changing

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

to

<script src="../../../JS/Registration.js" language="javascript" type="text/javascript"></script>
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
rahul
  • 184,426
  • 49
  • 232
  • 263
  • 1
    I am sure, file is present at this path. – Vaibhav Jain Nov 03 '09 at 11:46
  • 1
    Maybe you're using master pages or something else and not quite sure where the files are launched. Simply open the file in the browser, press "view source" and use the JS path in the URL to see if it opens. Your syntax is (more or less) correct. – Faruz Nov 03 '09 at 11:48
  • 1
    `type="text/javascript"` is also not necessary here – ˈvɔlə Feb 18 '14 at 12:23
5

I assume that you are using MasterPage so within your master page you should have

<head runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

And within any of your pages based on that MasterPage add this

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="js/yourscript.js" type="text/javascript"></script>
</asp:Content>
NoWar
  • 36,338
  • 80
  • 323
  • 498
4

ScriptManager control can also be used to reference javascript files. One catch is that the ScriptManager control needs to be place inside the form tag. I myself prefer ScriptManager control and generally place it just above the closing form tag.

<asp:ScriptManager ID="sm" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/Scripts/yourscript.min.js" />
    </Scripts>
</asp:ScriptManager>
th1rdey3
  • 4,176
  • 7
  • 30
  • 66
0

Use Fiddler to see what is happening. Then change the path accordingly. You will probably find you get a 404 error and the path is wrong.

RichardOD
  • 28,883
  • 9
  • 61
  • 81