Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3760454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:28:26+00:00 2026-05-19T10:28:26+00:00

Why does Javascript treat relative URLs differently than standard HTML? Think of the following

  • 0

Why does Javascript treat relative URLs differently than standard HTML? Think of the following URL (or just browse to it): http://en.wikipedia.org/wiki/Rome.
Open a Firebug console (or another Javascript console) and enter the following:

var x = new XMLHttpRequest();
x.open("GET", "foo", true);
x.send("bar");

Under my system the request is sent to “http://en.wikipedia.org/wiki/foo“. The “Rome” in the URL is simply ignored. The same request with a trailing slash in the URL (“http://en.wikipedia.org/wiki/Rome/“) appends the “foo” to the full URL.

This seems to make it pretty hard to encode the correct URLs in Javascript. Are there any Javascript libraries that help to overcome this problem?

(I asked a similiar question before, but more jQuery specific, where this also happens. I hope I get a better answer with this somewhat more library independent question.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:28:27+00:00Added an answer on May 19, 2026 at 10:28 am

    (updated to make it more readable)

    This is how relative paths is supposed to work.

    Pretend that the current address is this:

    Absolute: protocol://some.domain.name/dir1/dir2/filename


    If you specify only a new filename "foo", you get the same protocol, host and dirs, only the file name is changed:

    Relative: foo

    Absolute: protocol://some.domain.name/dir1/dir2/foo


    If you specify a whole path "/dir3/filename2" you get the same protocol and hostname but with another path:

    Relative: /dir3/filename2

    Absolute: protocol://some.domain.name/dir3/filename2


    You can also specify host name "//another.domain.name/dir5/filename3" and get the same protocol but another host, dir and filename:

    Relative: //another.domain.name/dir5/filename3

    Absolute: protocol://another.domain.name/dir5/filename3


    What might be confusing is that a webserver internally can add a / at the end of the url if the specified url points to a directory and not to a file.

    protocol://some.domain.name/somename

    If "somename" is a directory the webserver might translate it to (possible with a redirect)

    protocol://some.domain.name/somename/


    UPDATE

    As cameron said in a comment: For reference, see step 6 in section 4 of RFC 1808

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the following JavaScript script open a new window, but fails to scroll
Does javascript coding work better with any particular server language?
A response on SO got me thinking, does JavaScript guarantee a certain endian encoding
JavaScript does funky automatic conversions with objects: var o = {toString: function() {return 40;
Why does this javascript return 108 instead of 2008? it gets the day and
Why does the JavaScript function encodeURIComponent encode spaces to the hex Unicode value %20
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
In Javascript: How does one find the coordinates (x, y, height, width) of every
Does anybody know a way with JavaScript or CSS to basically grey out a
Does anyone have a library or JavaScript snippet to validate the check digit of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.