-2

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.

Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
jorot
  • 1
  • Hi @jorot and welcome to Stack Overflow. Could you give us a bit of context. What framework or libraries are you using, if any? What have you tried so far? Maybe show us a bit of code. I also suggest you take a look at this helpful resource on how to ask great Stack Overflow questions: https://stackoverflow.com/help/asking. – snowfrogdev May 23 '18 at 13:25

3 Answers3

0

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;

ref: https://stackoverflow.com/a/901144/905653

  • There are some links/URLs; every single URL is a 'complex' URL like this: _URL1_ + randomtext + **URL2** . What I want to do is...on click on every of those 'complex' URLs to open ONLY the **URL2** (each case different URL...), NOT the entire 'complex' URL . _Thank you_ . – jorot May 29 '18 at 07:13
0

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"));
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
0

...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"));
jorot
  • 1