将BASE HREF更改为绝对引用? [英] Change BASE HREF for absolute references?

查看:190
本文介绍了将BASE HREF更改为绝对引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我复制外部网页的大型html源代码(比如 http://www.foo .com / bar / something.html )到我个人电脑的目录(比如/ xxx)。文件'something.html'包含许多绝对引用,形式为href =/ bar / another.html或src =/ bar2 / yetanother.jpg等。



<如果我只需点击'something.html'(从我的浏览器以'file://'的形式访问它) - 或者即使我将它上传到我自己的服务器并通过'http://'访问它 - 全部这些引用将在文件所在的同一主机中查找。我仍然希望他们在原始主持人(即 http://www.foo。如果他们是相对引用(没有第一个斜杠),我会简单地把< base href = http://www.foo.com/ >在HEAD部分。 我怎样才能达到与这些绝对引用类似的效果?



还要考虑some.html包含许多其他文件(CSS, js,...),它也可能有这样的绝对引用...

解决方案

您有术语向后: code> /bar/another.html 是一个相对引用,而不是绝对引用。 / 指示在资源的根目录重新启动。对于 file:/// URLs,这将从文件系统的根目录开始,但它仍然是相对的。

如果添加< base href =...> ,它将预先添加 ... 除非URL确实是绝对的(开始于 http:// ftp:// file:// etc)

如果您使用基础href作为 file:// / where_i_downloaded / ,您将获得从那里链接的资源(不是来自文件系统的根目录),也可以是 http://www.foo.com/ 这会强制浏览器尝试从原始服务器加载(尝试,因为AJAX服务的URL可能不适用于此)。


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...

解决方案

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).

这篇关于将BASE HREF更改为绝对引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆