Search code examples
javascriptjqueryhtmlurlget

Show Parameter Segment of URL using JavaScript


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.


Solution

  • 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>