michaelderiso
michaelderiso

Reputation: 163

How do I append a query parameter to my URL using Javascript?

I am building a web app and I am using Firebase to store my user's data in Cloud Firestore. There is a page on my web app that allows users to view their documents from Cloud Firestore. I would like to add a query parameter to the end of my URL on view.html so I can take that query parameter value and use it to search for a document.

I have been searching online to find possible solutions. So far I have come across a few videos on the topic, but they haven't been going into the depth I have been needing. For example, this video shows how to add and get query parameters from a URL, but it only shows how to log those changes in the console. How would I make that my URL?

I've also be browsing Stackoverflow for solutions. This Stackoverflow post asks a similar question, however, many of the solutions in the answers causes view.html to reload on a loop. Why would this be, and if this is a possible solution, how would I stop this from happening.

How would I go about appending and fetching URL query parameters in Javascript?

Upvotes: 7

Views: 32636

Answers (3)

M. Richard
M. Richard

Reputation: 1

You can try to push state as so in the actual view.html

<script>
  const thisPage = new URL(window.location.href);
  window.history.pushState("id","id",thisPage);
</script>

Upvotes: 0

Stephen P
Stephen P

Reputation: 14830

You say you want to do this in javascript, so I assume the page itself is building/modifying a link to either place on the page or go to directly via javascript.

In javascript in the browser there is the URL object, which can build and decompose URLs

let thisPage = new URL(window.location.href);
let thatPage = new URL("https://that.example.com/path/page");

In any case, once you have a URL object you can access the parts of it to read and set the values.

Adding a query parameter uses the searchParams attribute of the URL, where you can add parameters with the .append method — and you don't have to worry about managing the ? and & … the method takes care of that for you.

thisPage.searchParams.append('yourKey', 'someValue');

This demonstrates it live on this page, adding search parameters and displaying the URL at each step:

let here = new URL(window.location.href);
console.log(here);
here.searchParams.append('firstKey', 'theValue');
console.log(here);
here.searchParams.append('key2', 'another');
console.log(here);

Upvotes: 15

michaelderiso
michaelderiso

Reputation: 163

I have solved this issue in the simplest way. It slipped my mind that I could link to view.html by adding the search parameter to the URL. Here's what I did:

On index.html where I link to view.html, I created the function openViewer();. I added the parameter to the end of URL href.

function openViewer() {
     window.location.href = `view.html?id={docId}`;
}

Then on view.html, I got the parameter using URLSearchParameters like so:

const thisPage = new URL(window.location.href);
var id = thisPage.searchParams.get('id');

console.log(id)

The new URL of the page is now "www.mysite.com/view.html?id=mydocid".

Upvotes: 0

Related Questions