How to include Javascript file in Asp.Net page

前端 未结 6 1186
一向
一向 2021-01-01 11:22

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

I tried using

相关标签:
6条回答
  • 2021-01-01 11:30

    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>
    
    0 讨论(0)
  • 2021-01-01 11:35

    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.

    0 讨论(0)
  • 2021-01-01 11:37

    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>
    
    0 讨论(0)
  • 2021-01-01 11:38

    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>
    
    0 讨论(0)
  • 2021-01-01 11:47

    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>
    
    0 讨论(0)
  • 2021-01-01 11:50

    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"));
    
    0 讨论(0)
提交回复
热议问题