在 PHP 中,您可以通过以下方式获取文档根目录:$_SERVER['DOCUMENT_ROOT']
。 Ej:
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,www.ejample.com/documents/examples/this-ex.html
。如果我只是调用 $("#example").load( path + file )
请求将是 documents/examples/+ example/directory
。就像我说的,这不是关于服务器端的问题。是关于获得正确的(和自动的)目录位置
最佳答案
您是否在寻找 document.location.hostname
?
var root = document.location.hostname;
$("#example").load( root + path + file );
关于javascript - jQuery:确定[服务器的]文档根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23944034/