views:

19

answers:

1
  1. I have 2 HTML files - file1.html in xyz directory and file2.html in abc directory ... both the directories are at different levels.
  2. Now, I have a external JS file in which I have used jQuery UIAautocomplete widget and I have used datasource URL for ajax call as a PHP file say datasource.php ...

Here I mentioned data source URL as absolute URL of that PHP file and I included that external js in file1, file2 mentioned in step 1, and everything works fine.

Now my issue is instead of using that datasource file as absolute ... how can use it as relative or can send it via some variable from the HTML files.. so that it can work fine from file1.html as well as file2.html even without using absolute URL.

A: 

I guess you can make use of browser's `location' object, like this:

baseURL = location.href;
baseURL = baseURL.substr(0, baseURL.lastIndexOf('/'));//we cut off the base file name, and the last /
baseURL = baseURL.substr(0, baseURL.lastIndexOf('/'));//we go another directory up
baseURL+= '/datasource.php';

suppose location.href was equal to 'http://localhost/site/xyz/file1.html', now baseURL will be equal to 'http://localhost/site/datasource.php'.

aularon