Modify URL and add parameter in between the URL using javascript - javascript

I have a long list of urls to be modified.
For example the url is:
https://somedomain.com/?pid=24787CL22311&source=linkkit&url=https%3A%2F%2Fwww.demo.com
So in the above url I want to add subid=1233 just before the &source=linkkit
like this:
https://some.com/?pid=24787CL22311&subid=1233&source=linkkit&url=https%3A%2F%2Fwww.demo.com
I tried with append, set and replace but did not gave the specific result
Any suggestions ?

You can do something like:
let url = 'https://some.com/?pid=24787CL22311&subid=1233&source=linkkit&url=https%3A%2F%2Fwww.demo.com';
url = url.replace('&source=', '&subid=1233&source=')

Related

Force language switch with URL parameter

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

How to replace path parameter in URL NodeJS

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 to copy URL using PHP or JS only last part of domain like post name

How to copy URL using PHP or JavaScript. I only want last part of URL like from this URL "https://stackoverflow.com/questions" i need "questions" only part to be shown in Text box
parse_url() is your friend for PHP. See: http://php.net/manual/en/function.parse-url.php
In Javascript you can get it by:
window.location.pathname
This shows the string /question if the URL is:
https://stackoverflow.com/questions
Here is an example of this site:
const URL = window.location.pathname;
console.log(URL);

How to get parameter from url in js?

I want to get the location in a URL (localhost: port/: location). For example, if the URL is localhost:8080/show/myplace/ I want to extract myplace.
How to do this in javascript?
Try below code
var location = window.location.pathname.split('/').slice(-1)[0];
This will return the part of URI you want.
I think that :
window.location.pathname
should work
This will give you an array of each pathname in the url:
window.location.pathname.split('/')
https://jsfiddle.net/mhd28cre/ (check your console log when running the example)

Angular js - get url query params when url has # hash

I have an url like this:
site.co/asd#antani?id=9
How do i get id value from this url dynamically and get the new value when it changes?
i tryed :
console.log($routeParams); and i got Object {} in console
thanks
Since I don't know your full Env. I would go to this dirtection:
var s = $location.search('site.co/asd#antani?id=9');
console.log(s.$$search);
Output:
Object {site.co/asd#antani?id: "9"}
Hope it will help,
OK i think i fixed it and it was to change URL
so to make $routeParams works i needed to call
site.com/asd?id=9#antani
instead of
site.co/asd#antani?id=9
hope it can help also if i don't like the url in that way :D
big thanks to all and if you have any better solution let me know please !
Got a gist to do that and other useful functions on urls with params ( URL params to object, Test if url Has Params, get Hash without params, and Change url params)
https://gist.github.com/dazzer13/10470514
You can do this:
var params = urlGetParams(url);
params.id // returns 9
var myURL = "site.co/asd#antani?id=9";
var myURL_id = myURL.split("#")[1];
Assuming this is the actual page you are on, you can use angular $routeParams. Something like this:
var id = $routeParams.id

Categories