How to redirect on another page and pass parameter in url from table ? I've created in tornato template something like this
<table data-role="table" id="my-table" data-mode="reflow"> <thead> <tr> <th>Username</th> <th>Nation</th> <th>Rank</th> <th></th> </tr> </thead> <tbody> {% for result in players %} <tr> <td>{{result['username']}}</td> <td>{{result['nation']}}</td> <td>{{result['rank']}}</td> <td><input type="button" name="theButton" value="Detail" ></td> </tr> </tbody> {% end %} </table>
and I would like when I press detail to be redirect on /player_detail?username=username
and show all detail about that player. I tried with href="javascript:window.location.replace('./player_info');"
inside input tag but don't know how to put result['username'] in. How to do this ?
To redirect to another page in JavaScript, you can use window. location. href property, window. location will also work and if you want you can use window.
You can use URL Redirect to forward your visitors to a specific page at the destination URL and pass values via query strings to the destination.
The simplest way to redirect to another URL is to use an HTML <meta> tag with the http-equiv parameter set to “refresh”. The content attribute sets the delay before the browser redirects the user to the new web page. To redirect immediately, set this parameter to “0” seconds for the content attribute.
Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".
Do this :
<script type="text/javascript"> function showDetails(username) { window.location = '/player_detail?username='+username; } </script> <input type="button" name="theButton" value="Detail" onclick="showDetails('username');">
Set the user name as data-username
attribute to the button and also a class:
HTML
<input type="button" name="theButton" value="Detail" class="btn" data-username="{{result['username']}}" />
JS
$(document).on('click', '.btn', function() { var name = $(this).data('username'); if (name != undefined && name != null) { window.location = '/player_detail?username=' + name; } });
EDIT:
Also, you can simply check for undefined
&& null
using:
$(document).on('click', '.btn', function() { var name = $(this).data('username'); if (name) { window.location = '/player_detail?username=' + name; } });
As, mentioned in this answer
if (name) { }
will evaluate to true if value is not:
The above list represents all possible falsy values in ECMA/Javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With