Make a button submittable more than once

So I found a script that can generate your link when you want to, here it is (credits to the owner!)

function CreateAffiliateLink(F) {
  var findstring = "XXXXX";
  var ts = 'var replacewith=document.' + F.name + '.AffCode.value';
  eval(ts);
  if (replacewith.length < 1) {
    return;
  }
  var re = new RegExp(findstring, "g")
  for (i = 0; i < F.length; i++) {
    var s = new String(F.elements[i].value);
    if (s.length > 0) {
      var newstr = s.replace(re, replacewith);
      F.elements[i].value = newstr;
    }
  }
}
<form name="me">
  <p>
    Type your affiliate code in the box and click the button:
    <input type="text" name="AffCode" size="17">
    <input type="button" value="Personalize links with my affiliate code" onClick="CreateAffiliateLink(this.form)">
  </p>
  <p>
    Image link:<br>
    <textarea name="a" cols="46" rows="3" wrap="off"> 
    <a href="http://example.com/master/#XXXXX 
    <img src="http://example.com/image.jpg"> 
    </a> 
    </textarea>
  </p>
  <p>
    An ezine text link:<br>
    <input type="text" name="b" size="46" value="http://example.com/master/#XXXXX">
  </p>
</form>

But right now, when I use it, I have to refresh everytime I want to put a new value. I wanted to find a way on how I can generate more than once without having to refresh the page.

I tried editing some part of it but it's still only usable once. Is there a way to change that?

2 answers

  • answered 2017-08-17 17:01 reski

    It's because findstring is always 'XXXXX' (it looks for value, which changes after click).

    Replace old code with:

    var findstring = "XXXXX";
    function CreateAffiliateLink(F) {
        var replacewith = F.AffCode.value;
        if (replacewith.length < 1) return;
        var re = new RegExp(findstring, "g")
        for (i = 0; i < F.length; i++) {
            var s = F.elements[i].value.toString();
            if (s.length > 0) {
                var newstr = s.replace(re, replacewith);
                F.elements[i].value = newstr;
                findstring = replacewith;
            }
        }
    }
    

  • answered 2017-08-17 17:01 Barmar

    The reason it only works once is because it's looking for XXXXX in the URL, and replacing that with the affiliate code. But after you do a replacement, the URL no longer has XXXXX.

    Use a more general regular expression that will match the master/# in the URL, and replace everything after it.

    function CreateAffiliateLink(F) {
      var replacewith=F.AffCode.value;
      if (replacewith.length < 1) {
        return;
      }
      replacewith = 'master/#' + replacewith;
      var re = /master\/#\w+/g;
      for (i = 0; i < F.length; i++) {
        var s = new String(F.elements[i].value);
        if (s.length > 0) {
          var newstr = s.replace(re, replacewith);
          F.elements[i].value = newstr;
        }
      }
    }
    <form name="me">
      <p>
        Type your affiliate code in the box and click the button:
        <input type="text" name="AffCode" size="17">
        <input type="button" value="Personalize links with my affiliate code" onClick="CreateAffiliateLink(this.form)">
      </p>
      <p>
        Image link:<br>
        <textarea name="a" cols="46" rows="3" wrap="off"> 
        <a href="http://example.com/master/#XXXXX"> 
        <img src="http://example.com/image.jpg"> 
        </a> 
        </textarea>
      </p>
      <p>
        An ezine text link:<br>
        <input type="text" name="b" size="46" value="http://example.com/master/#XXXXX">
      </p>
    </form>