5

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

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?

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

Omar
  • 11,783
  • 21
  • 84
  • 114
  • 2
    js is client side, so you cant –  May 29 '14 at 22:27
  • look into this question- may be what you are looking for http://stackoverflow.com/questions/1368264/get-host-name-in-javascript – aarti May 29 '14 at 22:28
  • @dagon if you carefully read my question, you'll see that this is not a server-sided issue/question. – Omar May 29 '14 at 22:29
  • 4
    And if you had a basic understanding of what you are talking about, then you would be aware that a DOCUMENT_ROOT is a purely server-side construct, which has no client-side meaning _whatsoever_ … – CBroe May 29 '14 at 22:30
  • The document root on the client site is very easy to get: `var root = '/';` – jeroen May 29 '14 at 22:38
  • Whenever I work on directories, on an HTML page, the "BASE" directory becomes the directory I'm currently at, 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. – Omar May 29 '14 at 22:41
  • 2
    _“Whenever I work on directories, on an HTML page, the "BASE" directory becomes the directory I'm currently at”_ – that’s how relative paths are resolved, yes. And simply putting a leading slash in front of your `path` would probably solve your problem without any further effort … – CBroe May 29 '14 at 22:46

1 Answers1

7

Are you looking for document.location.hostname?

var root = document.location.hostname;

$("#example").load( root + path + file );
Tom Studee
  • 10,316
  • 4
  • 38
  • 42