Grab URL Var and use in link

Discussion in 'JavaScript' started by LA Ricketson, Apr 19, 2016.

  1. #1
    Hello!

    I need code that will take the variable (8KWM2MPUSCMU4) from this link:

    http://laricketson.com/life-changing-webinar/?LARicketson&IP=8KWM2MPUSCMU4

    and add it to the end of my button link:

    https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=xxx
    (in place of the x's.)

    The code will be close to this I think, but I am not sure how to implement. Please advise:

    var first = getUrlVars()["ip"];

    alert(first);

    function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&#]*)/gi, function(m,key,value) {
    vars[key] = value;
    });
    return vars;
    }

    Can anyone help? Should only take a few minutes. Thank you!
     
    LA Ricketson, Apr 19, 2016 IP
  2. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #2
    Is laricketson.com your website? You can do it with PHP with $_GET. Explain technologies you are using.

    is 8KWM2MPUSCMU4 always this long? string with known number of characters at the end of URL?
     
    Blizzardofozz, Apr 19, 2016 IP
  3. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #3
    
    var inputUrl = "http://laricketson.com/life-changing-webinar/?LARicketson&IP=8KWM2MPUSCMU4",
       outputUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=xxx";
     
      function getID(inputUrl) {
      return inputUrl.substring(inputUrl.indexOf("IP=") + 3);
      }
     
      function putId(IdToAdd, outputUrl){
         return outputUrl.replace("xxx", IdToAdd);
      }
     
     
    alert(putId(getID(inputUrl), outputUrl));
    
    
    Code (markup):
     
    Blizzardofozz, Apr 19, 2016 IP
  4. fisasti

    fisasti Active Member

    Messages:
    42
    Likes Received:
    5
    Best Answers:
    2
    Trophy Points:
    58
    #4
    Hi! "IP=8KWM2MPUSCMU4" shows that the var name is IP, not 8KW... So you need to take core of the IP variable. If i were you i would a regular expression like:
    url = window.location.href;
    var expr = /IP=([^&]+)/;
    result = url.match(expr);
    alert(result);
    Code (markup):
    The result might be in result[1], you can check what the alert returns
     
    fisasti, Apr 19, 2016 IP