What is the best way to build a loopback URL for an AJAX call? Say I have a page at
http://www.mydomain.com/some/subdir/file.php
that I want to load with an AJAX call. In Firefox, using jQuery this works fine:
$.post('/some/subdir/file.php', ...);
Safari/WebKit tries to interpret this as a location on the local filesystem (so it ends up translating to ‘file://some/subdir/file.php’). Not only does this not point to anything useful, it also results in a security error.
Is there a way to accomplish this without hard-coding the domain into the URL? I’d like to make this as domain-independent as possible.
Update
I ended up parsing out the base url from location.href and throwing it into an accessible jQuery function like this:
/** * Retrieves the current root URL. * * @return string the root URL */ $.fn.rootUrl = function() { var url = location.href; return url.substring(0, url.indexOf('/', 7)); };
Wow, that’s pretty poor of Safari/WebKit, IMO.
You could observe document.href, count the slashes, and add that many ‘../’ to the beginning of your URL.