In requireJS, there is a property called baseURL. By setting the baseURL, we can write the file path to be loaded relative to the project instead of the current page. For example: suppose our project directory is /myproject/, and there are two pages in it, one is /myproject/one.html, the other is /myproject/html/other.html, and they both need to load /myproject/js/some.js. If we set the baseURL to /myproject/, then when we load the js, both pages can use the path ./js/some relative to the project, instead of using different relative paths because of different page paths. But if we don't use requireJS, can we achieve functionality similar to baseURL? base tag Copy code The code is as follows:<html> <head> <base href="http://static.cnblogs.com/" /> </head> <body> <img src="./images/logo_gray.gif" /> </body> </html> We will find that the image we loaded using the relative path ./images/logo_gray.gif becomes the image http://static.cnblogs.com/images/logo_gray.gif. The base tag can add a default path or a default opening method to the link of the page. Here is an example of setting the default opening method: Copy code The code is as follows:<html> <head> <base target="_blank" /> </head></p> <p><body> <a href="http://www.cnblogs.com">This page will open in a new window</a> <a href="http://justany.cnblogs.com">This page will also open in a new window</a> </body> </html> A BUG It is best not to write the base tag dynamically, otherwise there will be a small bug in Firefox and IE, such as for the page http://localhost/static/test.html: Copy code The code is as follows:<html> <head> <script> document.write('<base href="http://localhost/" />'); </script> </head></p> <p><body> <img src="static/1.jpg" /> </body> </html> Firefox and IE will load http://localhost/static/static/1.jpg first, and then load http://localhost/static/1.jpg. That is, they all try to load it with a path relative to the current page first, and then load it with the default path set by the base tag. Chrome loads fine. Firefox is not loading properly. Why? We haven't found a good explanation for this. Our analysis is that the browser optimizes resource loading, which causes the browser to pre-load resources when the dynamically inserted base tag does not take effect. As a result, an error occurs, and then the base tag takes effect and loads the correct resource. How to avoid it? There will be no problem if you do not write the base tag dynamically. If you need to write the base tag dynamically, all external resources referenced by the page need to be loaded dynamically through Javascript. |
<<: Detailed explanation of Vuex environment
>>: Navicat connection MySQL error description analysis
It has been a long time since the last update of ...
The general way of writing is as follows: XML/HTM...
There are 4 commonly used methods, as follows: 1....
Without further ado, I'll go straight to the ...
1. Under 800*600, if the width of the web page is...
This article shares the specific code for JavaScr...
need Configuring DingTalk alarms in Zabbix is s...
reduce method is an array iteration method. Unlik...
This time let’s look at a navigation bar layout w...
Effect display The built-in boot interface is too...
Table of contents Preface interface type Appendix...
This article example shares the specific code of ...
Table of contents 01 ReplicaSet Architecture 02 I...
FileReader is an important API for front-end file...
Table of contents 1. In project development, the ...