Master page JS, image Path Problems

Source: Internet
Author: User
A master page references a JS file and a CSS style sheet file. The path is the relative path used, and the code is like this.

View sourceprint?
1 <link href="../css/dtree.css" rel="stylesheet" type="text/css" />
2   
3 <script src="../js/dtree.js" type="text/javascript"></script>
View sourceprint?
1 I put the master page in the masterpage folder of the project file, and put the script file and CSS file in the corresponding folder, which is also visible in design mode. Then I add the content page, the content page assumes that I put the content under the root directory of the website, and then put the content into the background for programming. Basically, I want to see the effect after the content is completed. Then I debugged F5, but I found that JS didn't work, view the source file in IE and find that the CSS and JS paths at the reference have changed, as shown below:
View sourceprint?
1 <link href="css/dtree.css" rel="stylesheet" type="text/css" />
2   
3 <script src="../js/dtree.js" type="text/javascript"></script>
View sourceprint?
1 As you can see from this, the path of the referenced CSS file on the Content Page changes after the page is loaded and run, while the JS path does not change accordingly. In this way, the content page cannot correctly reference the JS file path, resulting in errors.
View sourceprint?
1 In fact, this problem is not limited to JS, such as the Basic HTML elements in the page. This problem also occurs when loading content pages due to the path of the relative path. To <a href ="http://www.cnblogs.com/sunrack/archive/2009/11/06/1597337.html" Target = _ blank> here </a> you can view a little more detailed information.
View sourceprint?
1 A better solution to this problem is to write it in this form.
View sourceprint?
1 <script src='<%=ResolveUrl("~/js/jquery.js") %>' type="text/javascript"></script>

The resolveurl method parses the server address. The converted address is an absolute path such as/project/folder/file.jpg. After conversion, you do not have to worry about the path problem caused by the master page.

 

 

 

1. image problems
Excellent solution

With runat = "server" added, the PATH uses the absolute path ~ /It is basically OK.
2. CSS file reference
<LINK rel = "stylesheet" type = "text/CSS" href = "style.css"/>
The magic here is that the href of The Link file is automatically adjusted with the directory structure of the accessed file. To put it bluntly, you can consider the path referenced by the master page.
For example, if the master page referenced by XXX. aspx in a folder under the same directory on the master page, the preceding statement automatically becomes
<LINK rel = "stylesheet" type = "text/CSS" href = "../style.css"/>
The premise is that 3. js file reference
<SCRIPT type = "text/JavaScript" src = "menu/jquery. Min. js"> </SCRIPT>
In this case, the client will not be able to reference the page files in other directories.
So you need to handle it like this
<SCRIPT type = "text/JavaScript" src = '<% = resolveurl ("menu/jquery. Min. js") %>'> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = '<% = resolveclienturl ("menu/ddsmoothmenu. js") %>'> </SCRIPT>
There are two methods, and the path on the client will be different, respectively:
<SCRIPT type = "text/JavaScript" src = '/tlerp/menu/jquery. Min. js'> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = '../menu/ddsmoothmenu. js'> </SCRIPT>
Obviously, resolveclienturl is a relative path, which is relatively easy to use. Of course, resolveurl also requires such an absolute path in many places, where tlerp is the name of a Web application.
In addition, if you put such code in Control contains code blocks (that is, <%... %>), so the control set cannot be modified.
The reason is that you can put the referenced content in <body>, even between <body> and <form>.

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.