6

I have an HTML file that contains breadcrumb links and a Javascript file that fires AJAX calls.

The breadcrumb links are relative to the HTML page. The AJAX calls are relative to the webapp context-path.

So for example, if the current HTML page is http://host.com/context_path/companies/5/user/10/index.html (where "5" and "10" are arbitrary IDs I don't know ahead of time):

If I set <base href="http://host.com/context_path/"> then the AJAX calls work fine, but I have no way of resolving the breadcrumb links relative to the HTML page.

Is there any way for me to implement this using client-side relative links? Or am I forced to resolve the links on the server side?

Gili
  • 86,244
  • 97
  • 390
  • 689
  • Having to separate `base` elements is not possible (and I’d consider even one bad enough, since I found they usually create more problems than they solve). Any reason why can’t you just simply specify the _correct_ path, either absolute or relative? – CBroe May 15 '14 at 01:50
  • @CBroe: I don't see how I can set the correct path using *client-side* resolution. I've updated the question, please take a look. – Gili May 15 '14 at 14:46
  • Why not? _You_ are outputting those relative links after all, so where is the problem in changing `wrong/relative/path` to `correct/relative/path` …? Totally unclear to me how that could be a problem. (Unless maybe you never handled relative paths correctly before, because you always relied on the `base` element … which one shouldn’t do IMHO, as I already said.) – CBroe May 15 '14 at 19:15
  • @CBroe, the HTML file in question is a static file returned by different URLs. The server is **not** rewriting its contents depending on which company id was referenced. If I set `` to the context path, then I am forced to specify the company id as part of the relative path (since `` stripped it off), but I have no way of doing this because the HTML file is static. If I could specify paths relative to the original location, then a relative path of `../../` would return the correct URL regardless of the company id. I hope you now understand what I mean. – Gili May 16 '14 at 05:04

2 Answers2

3

Answering my own question:

While it is true that the <base> tag affects all relative links, it looks like location.href points to the original HTML page [1]. So what you can do is set the <base> tag to satisfy the AJAX calls, and set the breadcrumb links relative to location.href using Javascript code.

[1] While I can't find any mention of this in the HTML specification, it seems to hold true for Chrome 34.0.1847.137m.

Gili
  • 86,244
  • 97
  • 390
  • 689
0

Unfortunately, there doesn't seem to be a way to do this. As specified in MDN:

Usage Note: If multiple elements are specified, only the first href and first target value are used; all others are ignored. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

Looks like the only way is to use javascript to convert your AJAX urls to full ones. There may be a way with $.ajax that allows you to intercept AJAX requests and manipulate the url before the request is made.

Modifying a jQuery ajax request intercepted via ajaxSend() may be a good starting point

Community
  • 1
  • 1
compid
  • 1,313
  • 9
  • 15