admin管理员组

文章数量:1318580

I have a script containing PHP and JavaScript files. All these files are in the root folder /myfolder.

Let's say the script I have to include on my website is /myfolder/script.js , the problem is that in script.js I have ajax calls to ../myfolder/ajax.php, which, because the path will be relative to the page the script is included won't work if I had something like this on the page /a/b/page.php:

<script src="../../myfolder/script.js><script> because this will call the ajax method from ../myfolder/ajax.php as stated in the AJAX call, which in this case is /a/myfolder/ajax.php.

How can I rewrite the AJAX call URL so that it will always point to the right file regardless where the script.js is included?

ROOT
|---myfolder
    |--script.js
    |--ajax.php
|--page1.php
|--subfolder
   |--page2.php
   |--subfolder
      |--page3.php

I have a script containing PHP and JavaScript files. All these files are in the root folder /myfolder.

Let's say the script I have to include on my website is /myfolder/script.js , the problem is that in script.js I have ajax calls to ../myfolder/ajax.php, which, because the path will be relative to the page the script is included won't work if I had something like this on the page /a/b/page.php:

<script src="../../myfolder/script.js><script> because this will call the ajax method from ../myfolder/ajax.php as stated in the AJAX call, which in this case is /a/myfolder/ajax.php.

How can I rewrite the AJAX call URL so that it will always point to the right file regardless where the script.js is included?

ROOT
|---myfolder
    |--script.js
    |--ajax.php
|--page1.php
|--subfolder
   |--page2.php
   |--subfolder
      |--page3.php
Share Improve this question edited Feb 5, 2013 at 13:31 XCS asked Feb 5, 2013 at 12:51 XCSXCS 28.2k28 gold badges104 silver badges153 bronze badges 10
  • Why don't you just call ajax.php? – fragmentedreality Commented Feb 5, 2013 at 13:02
  • That will call /a/b/ajax.php which is not the correct path. – XCS Commented Feb 5, 2013 at 13:08
  • 1 Could you provide a simple graphic representation of your folder/file structure? I'm getting headaches trying to grasp this in text form. – MCL Commented Feb 5, 2013 at 13:17
  • @MCL, sure, it confuses me too. – XCS Commented Feb 5, 2013 at 13:28
  • Done. This should work if script.js is included from any page.php. – XCS Commented Feb 5, 2013 at 13:31
 |  Show 5 more ments

1 Answer 1

Reset to default 7
/**
 * returns the current context path,
 * ex: http://localhost:8080/MyApp/Controller returns /MyApp/
 * ex: http://localhost:8080/MyApp returns /MyApp/
 * ex: https://www.example.co.za/ returns /
 */
function getContextPath() {
    var ctx = window.location.pathname,
        path = '/' !== ctx ? ctx.substring(0, ctx.indexOf('/', 1) + 1) : ctx;
    return path + (/\/$/.test(path) ? '' : '/');
}

on a URL like http://www.example./ajax:

getContextPath() + 'myfolder/ajax.php'

will return /ajax/myfolder/ajax.php


similarly on a URL like http://www.example./:

getContextPath() + 'myfolder/ajax.php'

will return /myfolder/ajax.php


lastly on a URL like http://www.example./myfolder/a/b/c/d/e/f/g/:

getContextPath() + 'myfolder/ajax.php'

will return /myfolder/ajax.php

本文标签: JavaScript relative AJAX call pathStack Overflow