简体   繁体   中英

Copy URL parameter and paste on HTML

I have this " parameter " %%name%% working, this %%name%% will be replaced with the user name on my URL.

My URL will be:

http://www.inversa.com/XV-MI-LJI-GLP-AIN-20181101-ADEA-PSNL-PR3-X/ Marcus

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

Example: Hello, Marcus .

If the URL length will be ALWAYS the same (ie after the 66 character it will display the value you want), you can easily achieve that with slice(). Working example:

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);

Hope that helps!

Read more about JavaScript methods regarding tto strings here https://developer.mozilla.org/cs/docs/Web/JavaScript/Reference/Global_Objects/String

Good luck!

Note: in order to get the URL replace it with that line:

var str = window.location.href;

This will be good for your particular use case, but i think you should get that name from other source...

you can try this code:

  <!DOCTYPE html> <html lang="en"> <head> <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/Marcus"; var name=url.split("/").pop(-1); document.getElementById('myH1').innerHTML=name;//put into h1 the 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