javascript jQuery:确定 [服务器的] 文档根目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23944034/
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-10-28 01:51:55  来源:igfitidea点击:

jQuery: determine the [server's] Document Root

javascriptphpjquerydocument-root

提问by Omar

in PHP you can get the document root by: $_SERVER['DOCUMENT_ROOT']. Ej:

在PHP中,你可以得到的文档根:$_SERVER['DOCUMENT_ROOT']。Ej:

PHP

PHP

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

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

...etc
?>

How can I get the value of $_SERVER['DOCUMENT_ROOT']in jQuery/JavaScript?

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

jQuery

jQuery

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

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

Notes: Whenever I work on directories, on an HTML page, the "BASE" directory becomes the directory of the current file. Ej, www.ejample.com/documents/examples/this-ex.html. If I just call $("#example").load( path + file )the request would be documents/examples/ + example/directory. Like I said, it's not a question about server-side stuff. Is about getting a correct (and automatic) directory position

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

回答by Tom Studee

Are you looking for document.location.hostname?

你在找document.location.hostname吗?

var root = document.location.hostname;

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