Saher Ahwal
Saher Ahwal

Reputation: 9237

Javascript redirect and Pass Argument to redirected Page

I have a main page that contains two buttons. Each of the buttons should redirect to the same page, however, the page that you are redirected to should do different things depending on the button you pressed. How can I redirect the user to that new page and pass in an argument depending on which button you have clicked?

Upvotes: 2

Views: 13486

Answers (2)

SeanCannon
SeanCannon

Reputation: 77956

Cross-browser event handling is much simpler with a toolkit like JQuery:

HTML:

<button data-param="foo">Foo</button>
<button data-param="bar">Bar</button>

JQuery code:

$('button').click(function(){
    window.location = window.location.href + '?param=' + $(this).data('param');
});

Demo: http://jsfiddle.net/6DK5J/

Upvotes: 5

Clive
Clive

Reputation: 36957

It depends what you mean by 'pass in an argument' but if you mean by URL query string then you can simply set up a form with an action to the page in question with a GET method:

<form action="page.html" method="GET">
  <input type="submit" name="submit1" value="Submit1" />
  <input type="submit" name="submit2" value="Submit2" />
</form>

The redirected URL will be page.html?submit1=Submit1 if the first button is clicked and page.html?submit2=Submit2 if the second button is clicked.

Upvotes: 1

Related Questions