Reputation: 5522
What is the best practice to provide Javascripts with URL's for things such as Ajax calls?
My javascripts live in the public folder website/js/
and need to get the website/
part so that I can structure the URL for my Ajax calls from there.
Is there any best methods for doing this? I was thinking of having something in the html such as:
var base = ;Are there better methods?
Upvotes: 1
Views: 48
Reputation: 198
Just use window.location.origin
If you need the slash "/" at the end:
var baseUrl = window.location.origin + "/";
Upvotes: 2
Reputation: 57
Just use the below code
var base_url='http://'+window.location.host+'/';
Upvotes: 0
Reputation: 42099
You don't need the fully qualified path. Start your URL with a /
and use relative paths.
var full_path = 'http://www.google.com/js/some_js_file.js';
var relative_path = '/js/some_js_file.js';
These are equivalent if you're viewing a page at www.google.com
Upvotes: 1