'How to redirect on another page and pass parameter in url from table?
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 ?
Solution 1:[1]
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:
- null
- undefined
- NaN
- empty string ("")
- 0
- false
The above list represents all possible falsy values in ECMA/Javascript.
Solution 2:[2]
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');">
Solution 3:[3]
Bind the button, this is done with jQuery:
$("#my-table input[type='button']").click(function(){
var parameter = $(this).val();
window.location = "http://yoursite.com/page?variable=" + parameter;
});
Solution 4:[4]
Here is a general solution that doesn't rely on JQuery. Simply modify the definition of window.location.
<html>
<head>
<script>
function loadNewDoc(){
var loc = window.location;
window.location = loc.hostname + loc.port + loc.pathname + loc.search;
};
</script>
</head>
<body onLoad="loadNewDoc()">
</body>
</html>
Solution 5:[5]
HTML - set an id attribute
<input type="button" id="go-another-page" name="theButton" value="Detail">Go to another page with parameters</td>
JS - Create an action listener for redirecting
const anotherPackage = document.getElementById('go-another-page');
anotherPackage.addEventListener('click', (event) => {
event.preventDefault();
// After ? mark set your key and variable eg: payment=order-consulting
// For multiple parameters you can use & eg: payment=order-consulting&amount=20
window.location.replace('/payment.html?payment=order-consulting');
});
Retrieve parameters from another page (In this case, payment.html)
// payment.js - this is javascript of your another page
document.addEventListener('DOMContentLoaded', (event) => {
const parameters = new URLSearchParams(window.location.search);
const payment = parameters.get('payment');
console.log(payment);
event.preventDefault();
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | Ravinder Singh |
Solution 3 | Atticus |
Solution 4 | TennisVisuals |
Solution 5 | MD SHAYON |