Access Usercontrol Javascript from the content home page

Hi everyone, I have a problem. I have a master page from which all my content pages are inherited. On this main page, I have a script tag pointing to a Javascript file folder~/Scripts/validation.js

On my content pages, I use various user controls that require the use of many functions in the file, validation.jshowever, unless I put <script>Javascript functions in the content holder on the content page, user controls do not see these functions. and I get errors as OnNameValidationnot defined.

Of course, I can copy Javascript code to all pages except 30+ pages and a maintenance nightmare if I find an error in one of the Javascript functions.

So the question (if you still haven’t understood from my long dissertation) is how can I declare a script tag with a path to the validation.js file so that the content pages, their user controls, etc. Could access functions / code.

+3
source share
4 answers

, , , , javascript ( html-, ). , javascript : "~/Scripts/validation.js", HEAD runat = "server". runat = "server" . - "../scripts/validation.js"

javascript , .

0

ScottGu .

, Intellisense , false:

<% if (false) { %>
   <script src="/Scripts/validation.js" type="text/javascript"></script>
<% } %>
0

The path that you assign to your js file is probably not consistent on all pages.

script src="../JavaScript/Scriptaculous/scriptaculous.js"

It should have something like the above if you have a separate folder for scripts, master pages, pages and controls.

0
source

I am currently doing this on my site by going to the source code on the main page and putting the following in the head and outside the ContentPlaceHolder.

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

    <script src="CP.js" type="text/javascript"></script>
</head>
0
source

Source: https://habr.com/ru/post/1704550/


All Articles