in the requireJS, there is an attribute called baseURL, by setting the baseURL, we need to load can be written in the file path relative to the project, rather than relative to the current page.

example: suppose we project directory is/myproject/, there are two pages, one is/myproject/one. The HTML, and the other one is/myproject/HTML/other. HTML, they all want to load/myproject/js/some js. If we set the baseURL into/myproject/, we at the time of loading the js, the two pages can be used relative to the path of the project./js/some, not because the page of different paths and use different relative path.

but if you don't use requireJS, can we realize the function similar to baseURL?

base tag
in fact, one of the HTML base tag, can have a similar function. For example:

 duplicate code  code is as follows: 
<Html>
<Head>
<Base href="http://static.cnblogs.com/"/>
</head>
<Body>
<Img SRC="http://www.jb51.net//web/images/logo_gray.gif"/>
</body>
</html>

we will find that we use relative path./images/logo_gray. GIF loading pictures, turned into http://static.cnblogs.com/images/logo_gray.gif.

the base label can be added to the page link of the default path, or the default open way.

here's a way to set the default open example:

 copy 

code is as follows:
<Html>
<Head>
<Base target="_blank"/>
</head> </p><P> <Body>
<A href="http://www.cnblogs.com" > This page will open in new window </a>
<A href="http://justany.cnblogs.com" > This page will open in new window </a>
</body>
</html>

a BUG
base label dynamic writing is best not to, or in Firefox and Internet explorer will there is a small BUG, such as the page http://localhost/static/test.html:

 copy 

code is as follows:
<Html>
<Head>
<Script>
document. Write (' <base href="http://localhost/"/> ");
</script>
</head> </p><P> <Body>
<Img SRC="http://www.jb51.net//web/static/1.jpg"/>
</body>
</html>

in Firefox and Internet explorer will load http://localhost/static/static/1.jpg first, then load http://localhost/static/1.jpg. In other words, they all try to use relative to the current path of the page to load, and then through the base tag set the default path load.

Google Chrome load is normal.

Firefox doesn't load properly.

 

according to?

this we didn't find a good explanation, our analysis is the browser to load resources is optimized, when insert the base label does not lead to dynamic, prior to loading, the result error, and then the base label effect and load the correct resources.

 

how to avoid? dynamic writing base label then there is no any problem. If you need to dynamically into the base label, then the page through Javascript all references to external resources need to be dynamic load.

# 0 - #


This concludes the body part