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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:52:09+00:00 2026-06-13T19:52:09+00:00

I have fetched window.location into a variable. Now, I want to remove part of

  • 0

I have fetched window.location into a variable. Now, I want to remove part of the string from the href within the location object, but still retain the object.

In other words, I want to modify the location object, and still be able to use window.location.protocol and window.location.host, but those functions need to work on the modified object.

For example, something like this where my browser displays "https://my.domain.org/site":

var thisloc = window.location;
Modify the href within that object to "http://my.domain.org/othersite"

//now I want it to fetch "http" instead of "https" based on my modified object
var thisprot = thisloc.protocol;   

Will that work? If it does, it would be very nice. Otherwise, I have to parse the URL to get the protocol, host and pathname from a modified href, which would also accomplish the same goal.

  • 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-13T19:52:10+00:00Added an answer on June 13, 2026 at 7:52 pm

    Inspired by James Padosley’s post on Parsing URLs with the DOM!, the trick here is to use an anchor element (<a>) as a URL builder/parser. With any anchor element, if you set its href property to some URL, it will be parsed by the DOM. Thereafter, you can freely modify the parts of that URL via properties on the anchor element. Query the href property at any time to see the full URL.

    Using the example from the question…

    var builder = document.createElement("a");
    
    builder.href = "https://my.domain.org/site";
    builder.protocol = "http://";
    builder.pathname = "/othersite";
    
    console.log(builder.href); // http://my.domain.org/othersite
    

    At this point the anchor element’s href will now be http://my.domain.org/othersite, as requested. Here’s a JS Bin demonstrating it in action: http://jsbin.com/omoqen/1/edit.

    The beauty is that anchor elements have the same URL component properties as the window.location object:

    protocol
    host
    hostname
    port
    pathname
    search
    hash
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have fetched some data from a SQL database into a datagridview, but after
I have fetched images and text as listview from database.now on clicking the particular
I have string that look like Array that fetched from other webservice like this
I have two tables with list of urls fetched from different sources. I want
I have fetched a result from core data in my app and need to
I have fetched the contents of a table in array of object. And the
I have a hidden property which is fetched from ebean. how do i pass
I have a problem related to an array data which fetched from an external
I have a big list of over 20000 items to be fetched from DB
Hey friends i have use 2 text view now when i fetch data from

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.