开发者

Are URLs in JavaScript files relative to JavaScript location or browser? [closed]

开发者 https://www.devze.com 2023-03-08 04:14 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. 开发者_如何学C
Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_如何学C

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 3 years ago.

Improve this question

When I use AJAX request in JavaScript file like:

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

When I am on location (rewrited URL):

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

And JavaScript is located and linked in meta tags by:

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

Then does the request go to http://example/some/action/id/request.php or http://example/js/request.php?

If I change request URL to /request.php instead of request.php then would it go to http://example/request.php always?

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

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 main page and also URL-rewritted addresses like:

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

And I want that URL to request in JavaScript file will it work in all of those conditions, or should I put address like url: "../request.php" in JavaScript file?


URIs in JavaScript are only ever inserted into objects associated with that document, so are relative to the document URI and not the JS src URI.

a. The former

b. Yes

c. I'd very strongly suggest that you keep the distance of any path from the root the same across all your environments. Either set up virtual name hosting locally, or use something like Charles Proxy to rewrite your domain name over your local testing environment.


On our company's dev server we had a similar problem. We solved it by using subdomains rather then folders. So it's projectname.localhost/some/action/id/.

0

精彩评论

暂无评论...
验证码 换一张
取 消