Javascript 使用 jQuery 确定相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6283000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:05:59  来源:igfitidea点击:

Determining relative path with jQuery

javascriptjqueryajax

提问by Yarin

I have several wordpress sites installed in subfolders under a top level domain:

我在顶级域下的子文件夹中安装了几个 wordpress 站点:

  1. http://www.mydomain.com/site-a/
  2. http://www.mydomain.com/site-b/
  1. http://www.mydomain.com/site-a/
  2. http://www.mydomain.com/site-b/

I need to load an AJAX file in each of them, and I've used the following code:

我需要在每个文件中加载一个 AJAX 文件,我使用了以下代码:

// Initiate asynchronous load of xml data:
jQuery.ajax({
    type: "GET",
    url: "/wp-content/themes/mytheme/data.xml",
    dataType: "xml",
    success: parseDataXML
});

but that ends up searching for the file in the domain's root path:

但这最终会在域的根路径中搜索文件:

http://www.mydomain.com/wp-content/themes/mytheme/data.xml

Instead of the site's root path:

而不是站点的根路径:

http://www.mydomain.com/site-a/wp-content/themes/mytheme/data.xml

回答by Adrian J. Moreno

Get rid of the leading "/" in the url path. The leading slash means "from the root of the site", not "from my current folder".

去掉 url 路径中的前导“/”。前导斜杠的意思是“来自站点的根目录”,而不是“来自我当前的文件夹”。

Edit: Ok, then in the root index of each "site", you need to define BASE HREF to include that folder name. Then the leading slash should take into account that value instead of the site root.

编辑:好的,然后在每个“站点”的根索引中,您需要定义 BASE HREF 以包含该文件夹名称。然后前导斜杠应考虑该值而不是站点根。

回答by ecchymose

My best bet would be check location, like:

我最好的选择是检查位置,例如:

var pathname = window.location.pathname;

And then do some indexOf() to specify which WP installation to point to.

然后做一些 indexOf() 来指定要指向哪个 WP 安装。

回答by Code Maverick

I haven't tested it, but would something like this work?

我还没有测试过,但是这样的东西会起作用吗?

var baseUrl = "http://www.mydomain.com",
    pathToDataXml = "/wp-content/themes/mytheme/data.xml",
    siteDir = window.location.href.toString()
              .replace(baseUrl, "")
              .replace(pathToDataXml, "");

// Initiate asynchronous load of xml data:
jQuery.ajax({
    type: "GET",
    url: baseUrl + siteDir + pathToDataXml,
    dataType: "xml",
    success: parseDataXML
});

回答by Useless Code

It's because you started your url with /that makes it an absolute url. To make it a relative url change it to url: "wp-content/themes/mytheme/data.xml",.

这是因为你开始了你的网址,/这使它成为一个绝对网址。要使其成为相对网址,请将其更改为url: "wp-content/themes/mytheme/data.xml",.

回答by jfatal

Simplest solution:

最简单的解决方案:

url: window.location.pathname + "/wp-content/themes/mytheme/data.xml",

回答by Atif Tariq

If your javascript code is written within php file then use

如果您的 javascript 代码是在 php 文件中编写的,则使用

url: "<?php echo site_url(); ?>/wp-content/themes/mytheme/data.xml",

If calling AJAX request onclick from php file then use this in onclick

如果从 php 文件调用 AJAX 请求 onclick 然后在 onclick 中使用它

onclick="cat_ajax_get('some_value', '<?php echo site_url(); ?>');"

onclick="cat_ajax_get('some_value', '<?php echo site_url(); ?>');"

function cat_ajax_get(catID, ajaxurl) { 
  jQuery.ajax({
    type: "GET", 
    url: ajaxurl + "/wp-content/themes/mytheme/data.xml",
    dataType: "xml",
    success: parseDataXML
  });
}

回答by lonesomeday

Perhaps you could define a global variable (I think this is justified in this case) defining the root directory:

也许您可以定义一个定义根目录的全局变量(我认为在这种情况下这是合理的):

rootFolder = location.pathname.substr(0, 7) === '/site-a' ? '/site-a/' : '/site-b/';

You could then use this variable for your AJAX request:

然后,您可以将此变量用于您的 AJAX 请求:

jQuery.ajax({
    type: "GET",
    url: rootFolder + "wp-content/themes/mytheme/data.xml",
    dataType: "xml",
    success: parseDataXML
});

回答by Péter

Be aware that the window.location.pathname is noting to to with your directory structure. I admit that lots of the case it's work, but not always!
Just wrote it down because maybe somebody ended up here with MVC in her/his mind.

请注意 window.location.pathname 与您的目录结构无关。我承认很多情况下它是有效的,但并非总是如此!
把它写下来是因为也许有人最终在她/他的脑海中想到了 MVC。

回答by Gordon Daniel

I think what he is asking is if there is something like ms mvc Url.Content which will allow you to use ~/ to navigate to the sub-domain. I don't think there is anything like that in jquery, or at least I haven't ran across it. You may need to hard-code the subdomain into the url.

我想他要问的是是否有像 ms mvc Url.Content 这样的东西,它可以让你使用 ~/ 导航到子域。我认为 jquery 中没有类似的东西,或者至少我没有遇到过它。您可能需要将子域硬编码到 url 中。

回答by Arques

You could simply use '..' before de folder name:

您可以在文件夹名称之前简单地使用“..”:

url: "../wp-content/themes/mytheme/data.xml"

I hope it works in wp. It usually works for other cases.

我希望它适用于 wp。它通常适用于其他情况。