Dynamic Loading of image paths ensures the relative independence of JavaScript controls

Source: Internet
Author: User

At that time, three methods were considered:
1. Write the path directly in the js file, but if the reference page path level changes, it will be the same.
2. Write a class, place it in the css file in the topic package, or create a css file separately for the date control. However, other elements in the control do not need to use the class. It seems that it is a big question to create a css document separately.
3. After the above two methods are excluded, we naturally need to dynamically load the image path.
The key code is as follows:
Copy codeThe Code is as follows:
// Create a save path for global variables
Var imgRootUrl = "";

// Obtain the directory of the image path
Var strpath=w.doc ument. location. pathname; // The Directory section after obtaining the host address
Var thisUrlCount = strPath. split ('/');
Var hierarchyOfFolders = thisUrlCount. length-2; // get the directory level
For (iRoot = 0; iRoot {
ImgRootUrl + = "../";
}
ImgRootUrl + = "Images/"; // This Images can also be set as a parameter. Because all project image folders have fixed names, no parameters are set, but they still save the scalability.

After adding a chart, the control effect is quite beautiful:

The following attributes are attached to all specified parameter Doc ument. location:
Part after document. location. hash // # VS window. location. hash
Document. location. host // domain name + port number
Document. location. hostname // Domain Name
Document. location. href // complete URL
Document. location. pathname // directory (Application)
Document. location. port // port number
Document. location. protocol // network protocol (http :)
Document. location. search //? Part after

Related Article

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.