1

I know if I have a php page, I can pass variables to the next page through the url with $_GET. Such as:

http://www.w3schools.com/welcome.php?fname=Peter&age=37

yields:

$currentName = $_GET["fname"]
$currentAge = $_GET["age"]

Is there an equally easy way to do this in pure HTML (or javascript)?

9
  • 1
    HTML is not a programming language. It's sort of possible with Javascript. Commented Jun 28, 2013 at 19:14
  • How would you do it in javascript? Commented Jun 28, 2013 at 19:15
  • Don't waste your time.. just use PHP Commented Jun 28, 2013 at 19:15
  • 1
    Here is how you do it in Javascript: stackoverflow.com/questions/901115/… Commented Jun 28, 2013 at 19:16
  • Why would you want to if you're already using PHP even though it's not possible with html? Commented Jun 28, 2013 at 19:18

1 Answer 1

1

using jQuery you can get url parameters with window.location but parsing with regex

$.urlParameter = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

$.urlParameter('Parameter  name');

OR with JS

function urlParameter(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

urlParameter('Parameter  name');
Sign up to request clarification or add additional context in comments.

2 Comments

That works without jQuery if you do $={} first.
Thanks for it just in simple Javascript. This is what I needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.