Generate canonical / real URL based on base.href or location

Posted by blueyed on Stack Overflow See other posts from Stack Overflow or by blueyed
Published on 2010-05-14T17:21:06Z Indexed on 2010/05/14 17:24 UTC
Read the original article Hit count: 351

Is there a method/function to get the canonical / transformed URL, respecting any base.href setting of the page?

I can get the base URL via (in jQuery) using $("base").attr("href") and I could use string methods to parse the URL meant to being made relative to this, but

  • $("base").attr("href") has no host, path etc attributes (like window.location has)
  • manually putting this together is rather tedious

E.g., given a base.href of "http://example.com/foo/" and a relative URL "/bar.js", the result should be: "http://example.com/bar.js"

If base.href is not present, the URL should be made relative to window.location. This should handle non-existing base.href (using location as base in this case).

Is there a standard method available for this already?

(I'm looking for this, since jQuery.getScript fails when using a relative URL like "/foo.js" and BASE tag is being used (FF3.6 makes an OPTIONS request, and nginx cannot handle this). When using the full URL (base.href.host + "/foo.js", it works).)

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript