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 8623745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:21:27+00:00 2026-06-12T07:21:27+00:00

When doing AJAX request in JavaScript, is there any case where it could be

  • 0

When doing AJAX request in JavaScript, is there any case where it could be necessary to write

window.location.protocol + '//' + window.location.host

instead of simply

/

After all, files like images, scripts, and css are always specified relative so it should be the same when making ajax requests.

The reason I’m asking this is because I can’t find any official suggestion (e.g. from w3c) on how to do it.

  • 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-06-12T07:21:28+00:00Added an answer on June 12, 2026 at 7:21 am

    They’re not strictly the same, as window.location.protocol + '//' + window.location.host is missing the trailing slash to indicate the root directory. But for all intents and purposes, they can be used interchangeably.

    In practical usage, assuming you’re not using <base>, all of the following will point to the same place:

    window.location.protocol + '//' + window.location.host + '/'
    window.location.href
    '//' + window.location.host + '/'  //useful if you don't know the scheme and don't want to check
    '/'
    

    window.location.host contains the port number if it’s anything other than 80, so you shouldn’t have to worry about including that. It’s simpler and clearer to write '/', as it always means “the root directory of whichever server this page came from.” Things could get hairy if the URI contains something like username:password@ — I don’t know of a way to get that data using JS. Trying to reassemble a URI from individual components could cause problems if the code is migrated to an unusual environment like that.

    The formal definition for all of this is in RFC3986, which is not exactly light reading.

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

Sidebar

Related Questions

I am trying to fake synchronous JavaScript while doing an AJAX request. I have
I'm doing an ajax request through jQuery and for example, my URL is http://test.com/query.php?hello=foo
I am doing an ajax request when an anchor tag is clicked with an
I have this code for doing an ajax request to a webservice: var MyCode
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Im doing a simple ajax request like:` $.ajax({ type: 'POST', url: 'http://' + serverIP
I'm doing the simplest ajax request possible: public ActionResult SayHello() { return Content(YYAAAY); }
I am doing a simple $.ajax request: $.ajax({ type: POST, url: /Run/ + param1
I have an ajax request in my javascript that is sending data to a
I'm doing an Ajax request in which I have the server returning well-formed HTML,

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.