【问题标题】:jQuery: determine the [server's] Document RootjQuery:确定 [服务器的] 文档根目录
【发布时间】:2014-05-29 22:24:11
【问题描述】:

在 PHP 中,您可以通过以下方式获取文档根目录:$_SERVER['DOCUMENT_ROOT']。乙:

PHP

<?php
$root = $_SERVER['DOCUMENT_ROOT'];
$path = '/example/directory/';
$file = 'some-file.html';

include( $root . $path . $file );

...etc
?>

如何在 jQuery/JavaScript 中获取 $_SERVER['DOCUMENT_ROOT'] 的值?

jQuery

//Example:
var root = ????;
var path = 'example/directory/';
var file = 'some-file.txt';

$("#example").load( root + path + file );

注意:每当我处理目录时,在 HTML 页面上,“BASE”目录成为当前文件的目录。 Ej,@ 987654326@。如果我只打电话给$("#example").load( path + file ),请求将是documents/examples/ + example/directory。就像我说的,这不是关于服务器端的问题。是关于获得正确(和自动)的目录位置

【问题讨论】:

  • js是客户端,所以不能
  • 看看这个问题 - 可能是你要找的stackoverflow.com/questions/1368264/get-host-name-in-javascript
  • @dagon 如果您仔细阅读我的问题,您会发现这不是服务器端问题。
  • 如果你对你在说什么有一个基本的了解,那么你就会知道 DOCUMENT_ROOT 是一个纯粹的服务器端构造,它没有客户端意义 whatsoever …
  • “每当我处理目录时,在 HTML 页面上,“BASE”目录就变成了我当前所在的目录”——这就是解析相对路径的方式,是的.只需在您的 path 前面加上一个斜杠就可以解决您的问题,而无需任何进一步的努力……

标签: javascript php jquery document-root


【解决方案1】:

你在找document.location.hostname吗?

var root = document.location.hostname;

$("#example").load( root + path + file );

【讨论】:

  • 这既是你的答案,也是var root = '/'; CRroe 评论的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-14
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多