views:

2579

answers:

4

I have some code in a javascript file that needs to send queries back to the server. The question is, how do I find the url for the script that I am in, so I can build a proper request url for ajax.

Ie. the same script is included on /, /help /whatever and so on, while it will always need to request from /data.json. Additionally, the same site is run on different servers, where the / folder might be placed differently. I have means to resolve the relative url where I include the Javascript (ez-publish template), but not within the javascript file itself.

Are there small scripts that will work on all browsers made for this?

+2  A: 

document.location.href will give you the current URL, which you can then manipulate using JavaScript's string functions.

John Topley
This is not usefull for me, as I need to find the webapp root, which can vary from server to server (in this case developers machine setups).
Staale
Which server-side technology does the webapp use?
John Topley
apache, php and ez-publish in this case. However I want something in pure javascript that will work regardless off servseride technology.
Staale
+14  A: 

For this I like to put <link> elements in the page's <head>, containing the URLs to use for requests. They can be generated by your server-side language so they always point to the right view:

<link id="link-action-1" href="${reverse_url ('action_1')}"/>

becomes

<link id="link-action-1" href="/my/web/root/action-1/"/>

and can be retrieved by Javascript with:

document.getElementById ('link-action-1').href;
John Millikin
Ah of course. Seems I missed the simplest of answers :).
Staale
This is a great use of the link tag.
Jonathan Arkell
I was imganing a solution going through all script tags and finding my own script, then something relative to that. This is much easier though.
Staale
+2  A: 

There's no way that the client can determine the webapp root without being told by the server as it has no knowledge of the server's configuration. One option you can try is to use the base element inside the head element, getting the server to generate it dynamically rather than hardcoding it (so it shows the relevant URL for each server):

<base href="http://path/to/webapp/root/" />

All URLs will then be treated as relative to this. You would therefore simply make your request to /data.json. You do however need to ensure that all other links in the application bear this in mind.

Luke Bennett
A: 

I include the following code in my libraries main entry point (main.php):

/**
 * Build current url, depending on protocal (http/https),
 * port, server name and path suffix
 */
$site_root = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") 
 $site_root .= "s";
$site_root .= "://" . $_SERVER["SERVER_NAME"];
if ($_SERVER["SERVER_PORT"] != "80")
 $site_root .= ":" . $_SERVER["SERVER_PORT"];
$site_root .= $g_config["paths"]["site_suffix"];

$g_config["paths"]["site_root"] = $site_root;

$g_config is a global array containing configuration options. So site_suffix might look like: "/sites_working/thesite/public_html" on your development box, and just "/" on a server with a virtual host (domain name).

This method is also good, because if somebody types in the IP address of your development box, it will use that same IP address to build the path to the javascript folder instead of something like "localhost," and if you use "localhost" it will use "localhost" to build the URL.

And because it also detects SSL, you wont have to worry about weather your resources will be sent over HTTP or HTTPS if you ever add SSL support to your server.

Then, in your template, either use

<link id="site_root" href="<?php echo $g_config["paths"]["site_root"] ?>"/>

Or

<script type = "text/javascript">
var SiteRoot = "<?php echo $g_config["paths"]["site_root"]; ?>";
</script>

I suppose the latter would be faster.

nlaq