0

Sorry for this very basic question and this is my first time posting. I am trying to link the following jquery to each html page in a project:

$(document).ready(function() {
    $(".class").append("<p><a href='../../homepage.htm'>Home page</a></p>");
});

The jquery is stored in a separate folder so I just reference it from each HTML page. However, the HTML pages are stored in folders in a directory type structure so does the path ../..homepage.htm need to be different depending on where the HTML page is located in relation to homepage.htm?

If so, do I need a separate jquery to handle different relative paths? I suppose I could use the absolute path of homepage.htm but its location would need to be updated for each version of the project.

thanks for any help

igr
  • 10,199
  • 13
  • 65
  • 111

2 Answers2

0

If you are importing this JS into various HTML files across the site and those files have varying sub-directory structures, then hard-coding a relative path is not possible. You want to either hard code an absolute path or create some JS to figure out the appropriate path before appending it.

Bryan Lewis
  • 5,629
  • 4
  • 39
  • 45
0

Try using a root-relative URL that starts with a / character. It should look something like <a href="/directoryInRoot/fileName.html">link text</a>.

$(document).ready(function() {
    $( ".class" ).append( "<p><a href='/directoryInRoot/homepage.htm'>Home page</a></p>" ); 
});

Here is a more in-depth explanation: Having links relative to root?

Community
  • 1
  • 1
Ryan Dantzler
  • 1,124
  • 1
  • 8
  • 18