I have a webpage that has a search box and button. When you enter text into the search box it returns information on the page. I would like to figure out how I can enter text into the URL and have it auto-fill the search box on the page.
For instance, on google I would enter this URL: https://www.google.com/?q=TEST. Where “TEST” is the text that I want to search for in the search box.
My webpage doesn’t have multiple URL’s associated with it, everything happens on the same page and I use javascript to update the page results (no server side).
How can I make this possible to search in the search box through my URL.
I tried implementing the code below, and it works on my Visual Studio live server, but it doesn’t work when I upload it to my domain. I get an “Internal Service Error.” It seems like it wants a “path name” after the URL, and in my domain I don’t have any path. Or maybe I’m doing it all wrong in general.
I tried implementing the code below, and it works on my Visual Studio live server, but it doesn’t work when I upload it to my domain. I get an “Internal Service Error.”
It seems like it wants a “path name” after the URL, which I provide when running in my Visual Studio Live environment. In my domain, I don’t have any path, I am just entering www.mywebsite.com/?q=TEST. Is this where the problem is or is there something code wise that needs to be done?
function getQueryParam(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
// Check if the "q" parameter exists in the URL
const inputTextFromURL = getQueryParam('q');
if (inputTextFromURL) {
document.getElementById('searchInput').value = inputTextFromURL;
startSearch(); // Perform the search operation automatically
}