Reference script resources in mastpage

Source: Internet
Author: User

We usually reference script resources like this:

< Html >
< Head Runat = "Server" >
<ScriptType= "Text/JavaScript"SRC= "JS/script_include.js"> </Script>
</ Head >
< Body >
</ Body >
.
</ Html >

Of course, you can write <SCRIPT type = "text/JavaScript" src = "JS/script_include.js"> </SCRIPT> to any location on the page, but writing it in the head is a very standard practice, writing in this way has many advantages, such as the "visibility" issue.

Now we have masterpage, which is very good. As long as we reference some common script files in masterpage, we don't need to write every page.
If the script file is still referenced as above:
Masterpage. Master <% @ Master language = " C # " Autoeventwireup = " True " Codefile = " Masterpage. master. CS " Inherits = " Masterpage "   %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
< Html Xmlns = "Http://www.w3.org/1999/xhtml"   >
< Head Runat = "Server" >
<ScriptType= "Text/JavaScript"SRC= "JS/script_include.js"> </Script>
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >

</ Form >
</ Body >
</ Html >

The problem arises. If the above masterpage is used in a page of different directories (for example, admin/default. aspx,
<ScriptType= "Text/JavaScript"SRC= "JS/script_include.js"> </Script>
This section will be unblocked and placed in the generated page. Obviously, the reference fails.

We know that "~" This is a very good thing and I really want to use it. Can I write it like this:
<ScriptType= "Text/JavaScript"SRC= "~ /JS/script_include.js"> </Script>
Of course not. Only runat = server control "~", Writing like above will only remain intact ~ /JS/script_include.js Output to the client. Do you want to change it to this:
SRC="<%= Request. applicationpath%>/JS/script_include.js">
In this way, the compilation will fail. You can see<HeadRunat= "Server"> No? Which of the following cannot be used?Runat= "Server"Masterpage and theme depend on it. There is no way to move it out of the head.

< Head Runat = "Server" >
</ Head >
< Script Type = "Text/JavaScript" SRC ="<%=Request. applicationpath %>/JS/script_include.js" > </ Script >
< Body >
< Form ID = "Form1" Runat = "Server" >
.......

Now it works, but there are two problems. First, the reference is not placed in the head. Second, if youProgramPut it in the root directory, it will become// JS/script_include.js. That's too bad, and uugly.

Atlas provides us with an elegant solution. We can use it like that:

< Head Runat = "Server" >
<Atlas: scriptID= "Script1"Runat= "Server"Path= "~ /JS/script_include.js" />
</ Head >

The above Atlas: script is runat = server, so you can safely use it "~ ", And the reference is still in the head. Of course, you must first configure your program to use Atlas. For details, refer to the related content on atlas.asp.net.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.