Search code examples
jquerypathloadrelative-path

use relative path in jQuery load


I have a .load jquery running in a file which I am accessing from this url

http://example.com/site_folder/case-studies/firstcase

Now the file has been saved under this directory

site_folder (dir)
     assets (dir)
        case_study_content (dir)
             file.txt (file)

My code is

$( "#slider li#"+nextId ).load( "../assets/case_study_content/file.txt #pickup");

it doesn't load the file content.

Now if I keep the file under site_folder and do the load as

 $( "#slider li#"+nextId ).load( "file.txt #pickup");

it works fine.


Solution

  • When you use a relative path, it's irrelevant where you loaded the script from. The path is relative to the page that loaded the script.

    If your page is inside the site_folder folder, then you use this to load the file:

    $("#slider li#" + nextId).load("assets/case_study_content/file.txt #pickup");