Search code examples
javascriptjqueryurltumblr

What is the easiest way to read a single variable in a URL?


Using the Tumblr API, I'm constructing anchors to my posts using their ID numbers, with the idea that if I can read the variable from the URL, I can use another script to find a single post using that ID and construct it on my website, keeping my viewers on my page instead of leaving to go to Tumblr. What would be the easiest way to do that?

Here's what the URL would be as an example:

    nevermorestudiosonline.com/singlepost.php?id=123456789

Plain and simple, I want to read the ID number from the URL and store it to a variable to be used by the API call to get the post. I just don't know how to get the ID from URL to variable.


Solution

  • JavaScript Way

    var url = 'nevermorestudiosonline.com/singlepost.php?id=123456789';
    var temp = url.split('=');
    var id = temp[1];
    
    // Now you can play with this id and use it the way you
    // you want.
    console.log(id);`