Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative urls for Javascript files

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.

I.e., 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?

like image 996
Staale Avatar asked Sep 20 '08 17:09

Staale


People also ask

What is relative URL of a file?

A relative URL is a URL that only includes the path. The path is everything that comes after the domain, including the directory and slug. Because relative URLs don't include the entire URL structure, it is assumed that when linking a relative URL, it uses the same protocol, subdomain and domain as the page it's on.

How do you reference a file in JavaScript?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

How do I get the relative URL in HTML?

To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.


3 Answers

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;
like image 189
John Millikin Avatar answered Oct 23 '22 04:10

John Millikin


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

like image 24
John Topley Avatar answered Oct 23 '22 03:10

John Topley


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.

like image 22
Luke Bennett Avatar answered Oct 23 '22 05:10

Luke Bennett