Detailed explanation of the use of base tag in HTML

Detailed explanation of the use of base tag in HTML

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
In fact, there is a base tag in HTML that can produce similar functions. For example:

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

Recommend

Details on using JS array methods some, every and find

Table of contents 1. some 2. every 3. find 1. som...

Analysis of MySQL lock wait and deadlock problems

Table of contents Preface: 1. Understand lock wai...

Example of using javascript to drag and swap div positions

1 Implementation Principle This is done using the...

HTML+CSS to achieve surround reflection loading effect

This article mainly introduces the implementation...

Tutorial on installing MySQL with Docker and implementing remote connection

Pull the image docker pull mysql View the complet...

WeChat applet custom tabbar component

This article shares the specific code of the WeCh...

Best way to replace the key in json object

JSON (JavaScript Object Notation, JS Object Notat...

How to implement real-time polygon refraction with threejs

Table of contents Preface Step 1: Setup and front...

WeChat applet implements a simple handwritten signature component

Table of contents background: need: Effect 1. Ide...

How to keep running after exiting Docker container

Phenomenon: Run an image, for example, ubuntu14.0...

JavaScript Shorthand Tips

Table of contents 1. Merge arrays 2. Merge arrays...

Install mysql5.7.17 using RPM under Linux

The installation method of MySQL5.7 rpm under Lin...