0

So I am trying to get the last part of a link and show it in a div on the HTML. I'm using JavaScript and HTML to perform this; here's what I have so far:

<script type="text/javascript">
    var pageURL = window.location.href;
    var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
    document.getElementById("getLink").innerhtml = lastURLSegment;
</script>

<div id="getLink"></div>

And here is my link:

https://playmafia.000webhostapp.com/world/play/create/?id=m12345

However, after executing this, nothing happens or shows. And plus, this code, as far as I know, will retrieve "?id=m12345".

I am new to javascript so any help would be great in figuring this out. Also, this may need jQuery, I'm not completely sure, so I will accept answers that involve it.

Once again, how can I get the parameter of the URL (in the example, m12345) to display in the HTML under the div with id "getLink"? Thanks to anyone who can help.

aman.s
  • 86
  • 9

3 Answers3

0

Try by changing document.getElementById("getLink").innerhtml to document.getElementById("getLink").innerHTML

brk
  • 48,835
  • 10
  • 56
  • 78
0

I hope you need some changes in your code

document.getElementById("getLink").innerHTML = lastURLSegment;

TRY THIS ...

Akbar Soft
  • 1,028
  • 10
  • 19
0

Try This :

    var pageURL="https://playmafia.000webhostapp.com/world/play/create/?id=m12345";
    var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
    var value=lastURLSegment.substr(lastURLSegment.lastIndexOf("=")+1);
    document.getElementById("getLink").innerHTML = value;
   
<div id="getLink"></div>
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
  • This works great, thanks! I actually had to update the var pageURL to window.location.href, since the m12345 is a random parameter value, but I didn't specify that, and after that, your solution works perfectly. – aman.s Dec 30 '17 at 17:06