Change BASE HREF for absolute references?

2019-08-29 04:07发布

I copy a large html source of an external page (say, http://www.foo.com/bar/something.html) into a directory in my PC (say, /xxx). The file 'something.html' contains many absolute references in the form href="/bar/another.html" or src="/bar2/yetanother.jpg" etc.

If I simply click 'something.html' (accessing it from my browser as 'file://') -- or even if I upload it to my own server and access it via 'http://' -- all those references will be looked in the same host where the file is. I still want them to be looked in the original host (i.e., http://www.foo.com).

Had they been relative references (without the 1st slash), I would simple put <base href=" http://www.foo.com/"> in the HEAD section. How can I achieve a similar effect with those absolute references??

Consider also the case where something.html includes many other files (css, js, ...) which may also have such absolute references...

1条回答
倾城 Initia
2楼-- · 2019-08-29 04:38

You have the terminology backwards: the reference /bar/another.html is a relative reference, not an absolute reference. The / indicates to restart at the root of the resource. For file:/// URLs, this will start at the root of the filesystem, but it's still relative.

If you add the <base href="..."> it will prepend the ... to the URL, unless the URL is indeed absolute (begins with http://, ftp://, file:// etc)

If you use the base href as file:///where_i_downloaded/, you'll get resources linked from there (not from the root of the file system), or as http://www.foo.com/ which would force the browser to attempt to load from the original server (attempt, because URLs for AJAX services may not work with this).

查看更多
登录 后发表回答