I have a search module that isn't working because it adds parameters to the URL.
For instance this URL works....
https://dev.berry.com/?s=pineapple
but the URL that is given is...
https://dev.berry.com/?s=pineapple**&et_pb_searchform_submit=et_search_proccess&et_pb_include_posts=yes&et_pb_include_pages=yes**
is there javascript that will eliminate from the "&" onward? That way whatever the search term is (pineapple, Tokyo, sunshine) it will work.
To begin, there should be no error with passing multiple parameters, so if you can, check the backend to fix this error rather than relying on front-end validation
That being said, you might want to checkout the URL module.
const url = new URL("https://dev.berry.com/?s=pineapple**&et_pb_searchform_submit=et_search_proccess&et_pb_include_posts=yes&et_pb_include_pages=yes**");
const search = url.searchParams.get("s");
url.search = "?s=" + search;
console.log(url.toString());
Related
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?
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);
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".
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>
Is there a way of forcing a language switch by URL parameter using javascript?
I want that when I go to this site 'wwww.google.com/en' he will be in English,
and when I went to 'wwww.google.com/it' he will be in Italian.
I have a button with setLanguage function that does this, but I want it to force it also when I get directly from the URL.
That type of configuration of a single page is typically handled with a query string, not a separate path. Instead of this:
https://www.google.com/en
Do this:
https://www.google.com/?lang=en
The query string data are available in searchParams:
let params = (new URL(document.location)).searchParams;
let lang = params.get('lang');
with window.location.pathname you will get a USVString containing an initial '/' followed by the path of the URL, and to get the first item from the url you can do something like:
const langURI = window.location.pathname.split('/')[1]
You can get info about the USVString here
Im trying to write this function that gets a search string, and returns the first result it gets,
however in said result i get only the page url and some other basic info, no id,no page content.
i would like to get it all as a single json without making a another request.
And that approach also doesnt work ,
since it doesnt seem to be able to find the page even when i send only the title.
is it possible that i can extract the page information somehow without the images and links aswell?
my current function:
async function getVideosWikiInfo(term) {
if (!term) term = 'Nto'
const searchRes= await fetch(`https://en.wikipedia.org/w/api.php?&origin=*& action=opensearch&search=${term}&titles&limit=1`).then(res=>res.json())
const searchUrl= searchRes[3][0]
const pageTitle=searchUrl.substr(30,searchUrl.length-1)
console.log(pageTitle);
const res = await fetch(`http://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=${pageTitle}`)
console.log(res);
}
You can use a generator module instead of the titles parameter, however that means you'll have to use the search query module instead of the opensearch api.
The url would be something like
`http://en.wikipedia.org/w/api.php?action=query\
&prop=extracts&exintro=\
&generator=search&gsrsearch=${term}&gsrlimit=1\
&format=json`
Problem Statement: To replace path parameter in URL before making a call to API in NodeJS(Javascript) based API automation framework.
Given URL: https://api.spotify.com/v1/albums/{id}
{id} needs to be replaced with value Sunshine
Expected URL: https://api.spotify.com/v1/albums/Sunshine
I have seen certain questions in StackOverflow. But they are more related to replace query_params value but not to replace path_params.
I am trying to do with this approach, but this code does not work.
var href = new URL('https://api.spotify.com/v1/albums/{id}');
href.searchParams.set('{id}', 'Sunshine');
console.log(href.toString());
Any help to let me know how to do this would be of great help.
This cannot be done with the URL API, you'll have to do a string replacement instead:
var href = 'https://api.spotify.com/v1/albums/{id}'.replace('{id}', 'Sunshine');
console.log(href);
How I can get the current wildcard id and pass it to my $http.post route in vue?
Once I created a quiz information it will return a page with a new url
http://localhost:8000/question/index/quiz/3
Then when I want to do a post route with a name
Route::post('question/store/quiz/{quiz}');
Here is my Vue http request post method
this.$http.post('/question/store/'+ , input).then((response) => {
What will be id that I can pass after the + sign?
Well this is pretty hacky, but it'll work if your URLs are always going to be formatted like that. so what I'm doing here is using vanilla JS to get the URL pathname, parse the string by the / and turn it into an array, then grab the last index.
var locationString = location.pathname
var locationArray = locationString.split('/')
var quizId = locationArray[locationArray.length - 1];
The quizId variable is the wildcard you're looking for
You should note that this is going to break if you ever have any query parameters, such as a URL looking like: /index/quiz/3?v=2842