Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.load() and relative paths

.load() is giving me trouble. I'm working on a section loader project and I just can't seem to fetch the file that I need.

What I am trying to achieve: #sectionContainer is empty on document load, but on document ready it is 'filled' with Pages1.html. This is done by a JavaScript file sections.js. The JS file and the index.html are NOT in the same folder. Here is the site structure (I am running a lot of projects on my site)

  • main folder
  • Project 1
  • Project 2 (sectionLoaderTest/)
    • index.html
    • Pages1.html
    • Pages2.html
    • css/
    • js/
      • sections.js
  • Project 3
  • ...

And the code I use to load Pages1.html on ready:

$(document).ready(function () {
    $("#sectionContainer").load("../Pages1.html", function (response, status, xhr) {
        if (status == "error") {
            var msg = "An error occurred. Status code: ";
            $("#error").html(msg + xhr.status + ". Status text: " + xhr.statusText);
        }
    });
});

I have tried every possible method (/, ./, ., ../, ..) that I know of and nothing seems to work.

Does anyone know what I am doing wrong?

like image 782
Bram Vanroy Avatar asked May 15 '12 16:05

Bram Vanroy


People also ask

What are relative paths?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.

Should I use relative or absolute paths?

Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.

What are relative paths in Python?

What is a relative path in Python? A relative path in Python is a path that describes the location of a directory relative to the entry point where you run the Python script.

What is difference between relative path and absolute path?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).


2 Answers

./Pages1.html should work. Tested all accounts for them in the address bar.

like image 96
Marcus Recck Avatar answered Nov 15 '22 20:11

Marcus Recck


Your AJAX URLs should be relative to the page you're on, so you want "Pages1.html". What you have in the test case (..Pages1.html) will never work, as that's not a valid reference. (Did you mean to do ../Pages1.html?)

like image 41
Mathletics Avatar answered Nov 15 '22 21:11

Mathletics