Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path in Script src and Link rel keeps resolving to root URL

Tags:

javascript

I have a document located at http://localhost:8081/develop.

The index.html, located at http://localhost:8081/develop/index.html contains the following:

<html>
    <head>
        ...
        <link rel="stylesheet" href="./styles.min.css">
        <script src="./bower_components/jquery/dist/jquery.js"></script>
        <script src="./bower_components/moment/min/moment-with-locales.js"></script>
        <script src="./bower_components/elessar/dist/elessar.js"></script>
        <script src="./bower_components/EventSource/eventsource.js"></script>
        ...
    </head>
    <body>
        ...
    </body>
</html>

ALL of the files starts with a ./, telling the browser that it should search them in the current directory, for example: <script src="./bower_components/jquery/dist/jquery.js"></script> should resolve into http://localhost:8081/develop/bower_components/jquery/dist/jquery.js. That's obvious.

But it doesn't. Instead, they are being resolved into http://localhost:8081/bower_components/jquery/dist/jquery.js, throwing tons of errors in the console.

enter image description here

The same thing will happen if instead of using ./ in the beginning of the files, I'd remove the first slash: <script src="bower_components/jquery/dist/jquery.js"></script> also resolves into http://localhost:8081/bower_components/jquery/dist/jquery.js.

It feels like a very basic question but I really need that those files have relative paths and that those paths works as expected. So why aren't them?

like image 587
Bruno Finger Avatar asked Sep 16 '16 12:09

Bruno Finger


People also ask

What is relative path in URL?

A relative URL is a URL that only includes the path. The path is everything that comes after the domain, including the directory and slug. Because relative URLs don't include the entire URL structure, it is assumed that when linking a relative URL, it uses the same protocol, subdomain and domain as the page it's on.

How do I give a href a relative URL?

To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.

How do you define a relative path in HTML?

Relative File Path: It describes the path of the file relative to the location of the current web page file. Example 1: It shows the path of the file present in the same folder of the current web page file.


1 Answers

Reason can be tag <base href="/"> in the head section.

like image 51
Pavel Avatar answered Nov 09 '22 06:11

Pavel