简体   繁体   中英

Copy and Paste URL parameter on HTML Text

I have one parameter from ExactSales called %%name%% .

When I send one email to my subscribers the parameters are replaced by the user name. Let's suppose the user is Paul, in my email will be replaced with this name.

Example: Hello, Paul .

In these emails I have one button, with the URL, this URL is automaticly replaced too, because it has the parameter %%name%% too;

http://www.mysubscribers.com/XX-XX-20181101-XX-XX/ Paul

This URL opens one landing page with my content.

I need one script that copy the parameter after the last "/" from the URL and paste on my HTML code where I want.

Example: Hello, Paul .

Is there posible? I don't know to much about javascript, can someone help me with this?

var url = window.location.href;
var parameters = url.split('/');
console.log(parameters[parameters.length - 1])

or something like

var str = "http://www.inversa.com/XV-MI-LJI-GLP-AIN-20181101-ADEA-PSNL-PR3-X/Marcus"; 
var res = str.slice(66);
console.log("Hello," + res);

And how Can I copy wherever I want in my HTML?

Hello, NAME

If you want to make the link more dynamic, assuming it always ends with a /name.

This code will separate the string in to an array at every slash, then get the last item of the array.

html code:

<p>Hello <span id="name"></span></p>

JavaScript code

$(document).ready(() => {
  getUrl();
});

function getUrl(){
  var url = window.location.href;
  var str = url.split('/');
  var name = str.pop();
  $('#name').html(name);

}

if i understood your question you can use this code:

  <!DOCTYPE html> <html lang="en"> <head> <!-- copy last item of a link es. http://www.mysubscribers.com/XX-XX-20181101-XX-XX/Paul you get Paul --> <meta charset="utf-8"> <title> test</title> <script type="text/javascript"> document.addEventListener("DOMContentLoaded", function(event) { var url="http://www.mysubscribers.com/XX-XX-20181101-XX-XX/Paul"; var name=url.split("/").pop(-1); document.getElementById('myH1').innerHTML=name;//put into h1 the name alert(name);//show alert with name }); </script> </head> <body> <h1 id="myH1"></h1> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM