I have a complex string (URL) that is a link .
How to OPEN on click
a specific URL from that URL/string... NOT the 'parent' URL?
Parent URL looks like this:
http://www.randomsite.com & randomtext & URL I need
.
Thank you.
I have a complex string (URL) that is a link .
How to OPEN on click
a specific URL from that URL/string... NOT the 'parent' URL?
Parent URL looks like this:
http://www.randomsite.com & randomtext & URL I need
.
Thank you.
On the assumption you mean get the url from the query string e.g. http://www.randomsite.com?foo=bar&url=http://www.url-i-want.com
You could do this:
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var url = getParameterByName('url');
window.location.href = url;
Here is a simple solution
function getUrlVar(stringUrl, name) {
for (var value of stringUrl.split("?")[1].split("&")) {
var valueArr = value.split("=");
if (valueArr[0] === name) {
return valueArr[1];
}
}
return false;
}
var complexUrl = "http://www.whatever.com?lang=en&url=http://www.anyurlwhatsoever.com&test=loremipsum";
console.log(getUrlVar(complexUrl, "test"));
console.log(getUrlVar(complexUrl, "lang"));
console.log(getUrlVar(complexUrl, "url"));
console.log(getUrlVar(complexUrl, "asdasd"));
...Almost done... ! ;-)
But...I will be more 'specific':
ParentURL = URL1+random-text+URL-I-want
What I want to do is...on click on the link/ParentURL to open the link/URL-I-want, NOT the link/ParentURL (I think the best "suggestion" is...to 'specify' somehow a 'part' of the URL1...to be sure the URL of the opened window will be URL-I-want, NOT the URL1) !
Thank you .
function getUrlVar(stringUrl, name) {
for (var value of stringUrl.split("?")[1].split("&")) {
var valueArr = value.split("=");
if (valueArr[0] === name) {
return valueArr[1];
}
}
return false;
}
var complexUrl = "http://www.whatever.com?lang=en&url=http://www.anyurlwhatsoever.com&test=loremipsum";
console.log(getUrlVar(complexUrl, "test"));
console.log(getUrlVar(complexUrl, "lang"));
console.log(getUrlVar(complexUrl, "url"));
console.log(getUrlVar(complexUrl, "asdasd"));