Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why base tag does not work for relative paths?

Tags:

html

base

tags

I have a <base> tag as below in <head> section of the page:

<base href="http://localhost/framework"> 

And a script as below which is relative (of course after <base>):

<script src="/assets/jquery-1.7.1.min.js"> 

But when I open jQuery from firebug it shows:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>404 Not Found</title> </head><body>  Blah Blah Blah.... 

When I use the below link it's OK though:

<script src="http://localhost/framework/assets/jquery-1.7.1.min.js">   

I looked for an answer everywhere, but it seems I'm doing my job right! So what is the problem?

like image 235
Alireza Avatar asked Jul 17 '12 10:07

Alireza


People also ask

Which tag is used to specify target of all relative URLs in page?

The <base> HTML element specifies the base URL to use for all relative URLs in a document.

How does base in HTML work?

Definition and UsageThe <base> tag specifies the base URL and/or target for all relative URLs in a document. The <base> tag must have either an href or a target attribute present, or both. There can only be one single <base> element in a document, and it must be inside the <head> element.

What is base URL and relative URL?

A URL specifies the location of a target stored on a local or networked computer. The target can be a file, directory, HTML page, image, program, and so on. An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point.

What is the purpose of base href XYZ?

The href attribute specifies the base URL for all relative URLs on a page.


1 Answers

/assets/jquery-1.7.1.min.js is not relative but absolute*, the / takes it to the root even with a base tag.

If you remove that /, it should make it relative off the current path, which, when a base tag is present would be http://localhost/framework/.

Note: You will also need to add a trailing / to the end of the href, to indicate that it's a folder.

Full working example:

<!doctype html> <html> <head> <base href="/test/" /> <script src="assets/test.js"></script> <body> hi </body> </html> 

* Actually depending on who you ask, it's still relative since it is relative off the current domain. But I prefer to call this absolute since it's signifying the path is from the root, based on the current domain. Although, I guess technically that makes it relative in the grand scheme of things, and absolute only in terms of the current domain. Whatever.

like image 180
Rudi Visser Avatar answered Oct 07 '22 19:10

Rudi Visser