Are the URLs in the JavaScript files relative to the location of the JavaScript or browser?

When I use an AJAX request in a JavaScript file, for example:

... url: "request.php" ... 

When I'm in place (rewritten URL):

 http://example/some/action/id/ 

And JavaScript is located and linked in meta tags with:

 ... src="http://example/js/ajaxrequest.js" ... 

Then, is the request sent to http://example/some/action/id/request.php or http://example/js/request.php ?

If I change the request URL to /request.php instead of request.php , will it always go to http://example/request.php ?

If I have two working environments, http://localhost/projectname/ and http://projectname.com/ , and the file structure always looks like this ( projectname.com is a copy of the directory with the project name):

 http://localhost/projectname/js/ajaxrequest.js http://localhost/projectname/request.php http://projectname.com/js/ajaxrequest.js http://projectname.com/request.php 

And I can be on the main page and also rewrite URLs such as:

 http://localhost/projectname/ http://projectname.com/ http://localhost/projectname/some/action/id/ http://projectname.com/some/action/id/ 

And I want this URL to be requested in the JavaScript file, will it work under all these conditions, or should I put an address like url: "../request.php" in the JavaScript file?

+4
source share
2 answers

JavaScript URIs are only inserted into the objects associated with this document, just like the document URI, not the src JS URI.

a. First

b. Yes

with. I would very strongly suggest that you keep the distance of any path from the root the same in all your environments. Either configure a virtual name on a local hosting, or use something like Charles Proxy to rewrite your domain name in a local test environment.

+5
source

We had a similar problem on our dev server. We solved this using subdomains, not folders. So projectname.localhost/some/action/id/ .

+1
source

All Articles