How to include Javascript file in Asp.Net page
Asked Answered
P

6

29

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.

Presentiment answered 3/11, 2009 at 11:41 Comment(3)
Can you explain 'it is not working'?Scudo
FYI <script> tags aren't self closing. You should be using </script>.Autoicous
Note that when you drag/drop jquery file into master page, the file name will be incorrect if you are in subfolder.Emmerich
S
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>
Scudo answered 3/11, 2009 at 11:43 Comment(3)
I am sure, file is present at this path.Presentiment
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.Misogamy
type="text/javascript" is also not necessary hereZwickau
H
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>
Hydroxylamine answered 3/11, 2009 at 11:48 Comment(1)
Thank you for this!Southeaster
G
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"));
Grits answered 3/11, 2009 at 11:57 Comment(0)
S
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>
Scudo answered 3/11, 2009 at 11:43 Comment(3)
I am sure, file is present at this path.Presentiment
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.Misogamy
type="text/javascript" is also not necessary hereZwickau
A
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>
Ambush answered 23/4, 2015 at 17:11 Comment(1)
Great answer if you are using master pages.Methyl
S
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>
Scrutator answered 25/1, 2017 at 6:59 Comment(0)
W
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.

Wohlert answered 3/11, 2009 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.