0

This script is already working but I want to know if it's possible to copy only the "parameter" after the "/" (slash).

My marketing campaigns adds diferents parameters after the "/" do know where the user came from, example: XV-MEL-OM-REVHL-X-X-POP-X-X.

This FORM is one POPUP, and i need to copy on a "hidden" input the parameter after the "/" to know where the user came from.

Script:

    <script type="text/javascript">
    function Copy() 
    {
        var Url = document.getElementById("paste-box");
        Url.value = window.location.href;
        Url.focus();
        Url.select();  
        document.execCommand("Copy");
    }

HTML:

    <form action="https://ws.inversapub.com/subscribe" method="POST" target="hiddenFrame">

  <input name="emailAddress" type="email" required="" oninput="this.setCustomValidity('')" oninvalid="this.setCustomValidity('Preencha um endereço de e-mail válido.')" placeholder="Coloque seu e-mail aqui" class="input-email-popup">

   <input type="submit" value="QUERO DOBRAR MEU DINHEIRO AINDA ESTE ANO" onclick="Copy();" class="btn-submit-popup om-trigger-conversion">
  <input type="hidden" name="sourceId" id="paste-box" value="XV-MEL-OM-REVHL-X-X-POP-X-X">

  <input name="listCode" type="hidden" value="inv_hotlist_revhl"/>

  <input name="redirect" type="hidden"  value="#" />

  <input name="email_page" type="hidden" value="inv_welcome_revhl"/>

</form>

My URL is: http://lp.inversapub.com/teste-lp-optin/ It's copying only the /teste-lp-option/

BTW, I'ts possible to add the parameter "XV-MEL-OM-REVHL-X-X-POP-X-X" if thats nothing after the "/"?

Hasta Dhana solution is working! But i need to add the parameters "XV-MEL-OM-REVHL-X-X-POP-X-X" if thats nothing after the "/"

Anselmo
  • 13
  • 5

3 Answers3

1

I think that you are looking for the location.pathname property of the Location API.

Code for your example:

function Copy() 
{
  var Url = document.getElementById("paste-box");
  Url.value = window.location.pathname;
  Url.focus();
  Url.select();  
  document.execCommand("Copy");
}

EDIT:

To add the default parameter you can test the location with a regular expression like this:

function Copy() 
{
  var Url = document.getElementById("paste-box");
  Url.value = window.location.pathname + (/\/$/.test(location.pathname) ? "XV-MEL-OM-REVHL-X-X-POP-X-X" : "");
  Url.focus();
  Url.select();  
  document.execCommand("Copy");
}
Chocolord
  • 493
  • 3
  • 12
  • It's not working! My URL is: http://lp.inversapub.com/teste-lp-optin/ It's copying only the /teste-lp-option/ I'ts possible to add the parameter "XV-MEL-OM-REVHL-X-X-POP-X-X" if thats nothing after the "/"? – Anselmo Oct 25 '18 at 15:00
  • I added a test with a regex to the answer, is it ok with it ? – Chocolord Oct 25 '18 at 15:24
  • My URL is: http://lp.inversapub.com/teste-lp-optin/ It's copying only the /teste-lp-option/ I tried to adapt to this below, but is taking the /teste-lp-optin/ too; – Anselmo Oct 25 '18 at 15:59
0

You may use window.location.pathname instead of window.location.href to take only the path part of the URL. If you need everything after the / (like GET parameters and hash) you can take the URL and remove the origin part:

window.location.href // http://example.com/path?get=value#bang
window.location.origin // http://example.com
window.location.href.slice(window.location.origin.length) // /path?get=value#bang

If you want to insert the whole URL but copy only the part of it you can look at selectionStart and selectionEnd properties. Here is a nice comment on it: https://stackoverflow.com/a/3085656/3641734.

termosa
  • 661
  • 6
  • 9
  • My URL is: http://lp.inversapub.com/teste-lp-optin/ It's copying only the /teste-lp-option/ I'ts possible to add the parameter "XV-MEL-OM-REVHL-X-X-POP-X-X" if thats nothing after the "/"? – Anselmo Oct 25 '18 at 15:00
0

You could use the lastIndexOf() function to locate the last portion of the / character in your URL :

function Copy() 
{
    var Url = document.getElementById("paste-box");
    Url.value = window.location.href.substr(window.location.href.lastIndexOf('/') + 1);
    Url.focus();
    Url.select();  
    document.execCommand("Copy");
}
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
  • It's working but if that's nothing after "/" send's an error. I'ts possible to add the parameter "XV-MEL-OM-REVHL-X-X-POP-X-X" if thats nothing after the "/"? – Anselmo Oct 25 '18 at 15:05