Getting Product ID from URL using JavaScript [duplicate] - javascript

This question already has answers here:
Last segment of URL with JavaScript
(30 answers)
How do I parse a URL into hostname and path in javascript?
(26 answers)
Closed 1 year ago.
i need to get the product id from an url into a var
https://instrumentall.md/ro/6167922/
can someone help me please?

In JavaScript use window.location.href to get the value of the url. Than process it as a normal string.

Related

How to replace a string in javascript only when it's comes before another string using regex? [duplicate]

This question already has answers here:
REGEX: Capture Filename from URL without file extension
(5 answers)
JavaScript Regexp to replace a value only when it follows certain characters?
(2 answers)
Closed 3 years ago.
I want to replace xx with . in this string 100x100/<name>xx-somename-xxjpg but only xx comes next to jpg.
'100x100/<name>xx-somename-xxjpg'.replace(/(xx(jpg|jpeg|png))/,'$')
What I get -> 100x100/<name>xx-somename-$
What I expect-> 100x100/<name>xx-somename-.jpg
How do I do it?
'100x100/<name>xx-somename-xxjpg'.replace(/(xx)(?=jpg|jpeg|png)/,'.')

How to get URLinformation after question mark? [duplicate]

This question already has answers here:
how to get url value after question mark in javascript
(2 answers)
Closed 3 years ago.
I have this simple code:
Subscribe today
when the user press on the a tag it takes him to the page subsc.php?English.
My question is how can i get the information after the ? (which is "English" in this example)?
You can try with Regex Positive Lookbehind /(?<=\?).$*/.
var url = '/subsc.php?English';
var r = url.match(/(?<=\?).*$/)[0];
console.log(r);

How to pass string with & via get in javascript [duplicate]

This question already has answers here:
Escaping ampersand in URL
(8 answers)
Closed 5 years ago.
I have a problem, I wonder if someone could help me, I need to pass a string that contains (&) via get. Example:
.com/index.php?nome=name=AA&BB&age=18&city=....
This way the name arrives from the other sides only AA
You have to encode the parameter before sending it to the server:
var url = "normal url?" + encodeURIComponent("get params");

How to set a variable based on the first string in the URL after a symbol? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
At the moment I am doing this:
var token = document.location.href.split('?_sft_category=')[1];
Which is ok if my URL is:
http://www.example.com/xchanges/results/?_sft_category=sky
I get sky in that case as a var, but if my URL becomes
http://www.example.com/xchanges/results/?_sft_category=sky#comboFilters%5BAgency%5D=.TBWA
In that case I obviously get everything after = while instead I would like to get ONLY the first string after '?_sft_category='
url = "http://www.example.com/xchanges/results/?_sft_category=sky";
alert(url.split('?_sft_category=')[1].match(/[A-z]+/));
url ="http://www.example.com/xchanges/results/?_sft_category=sky#comboFilters%5BAgency%5D=.TBWA";
alert(url.split('?_sft_category=')[1].match(/[A-z]+/));
Demo

get query string value using javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
Given the following string/query string:
ajax/hovercard/hovercard.php?id=100000472545907&extragetparams=%7B%22hc_location%22%3A%22stream%22%7D
What is the best way to extract the id?
Try this:
yoururl.match(/id=(.*)&/)[1]
Fiddle
params = location.search.substring(location.search.indexOf('id')).split('&')[0]
id = params.substr(3)
If it is always shipped in this exact order - then
url.match(/\d+/)
otherwise
url.match(/id=(\d+)/)

Categories

Resources