Dynamically loading picture paths keeping the relative independence of JavaScript controls _javascript tips

Source: Internet
Author: User
There were three ways to consider:
1. Directly write the path in the JS file, but if the reference page path level changes can not.
2. Write a class, put the CSS file in the subject package or create a separate CSS file for the date control. However, the other elements in the control do not use the needs of class, the separate creation of the corresponding CSS document seems to make a fuss.
3. After the above two methods are excluded, it is natural to adopt the method of dynamically loading the picture path.
The key code is as follows:
Copy Code code as follows:

Create a global variable save path
var imgrooturl = "";

Get the directory where the picture path is located
var strpath=window.document.location.pathname; Get the directory part after the host address
var thisurlcount = strpath.split ('/');
var hierarchyoffolders = thisurlcount.length-2;//Get directory hierarchy
for (iroot=0;iroot{
Imgrooturl + = ". /";
}
Imgrooturl + = "images/"; This images can actually be set as a parameter, because all items picture folders are named fixed, so there is no set parameter, but still save extensibility.

When you add a diagram, the control effect is pretty beautiful:

By the way, attach all window.document.location its subordinates:
Document.location.hash//#号后的部分 VS Window.location.hash
Document.location.host//Domain name + port number
Document.location.hostname//Domain
Document.location.href//Full URL
Document.location.pathname//directory section (application)
Document.location.port//Port number
Document.location.protocol//Network Protocol (http:)
Document.location.search//number of parts after

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.