Pushing selected daterange picker value to URL
Pushing selected daterange picker value to URL
I'm using a daterange picker in my website I need to push the selected start and end date to the URL which also has other parameters so while pushing the data it must not remove the other parameters. Please tell me what I must add do to the code so that I can do this. :) thanks in advance
PS I'm new Javascript.
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<div id="reportrange" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 10%">
<i class="fa fa-calendar"></i>
<span></span> <i class="fa fa-caret-down"></i>
</div>
<script type="text/javascript">
$(function()
var start = moment().subtract(29, 'days');
var end = moment();
function cb(start, end)
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
$('#reportrange').daterangepicker(
startDate: start,
endDate: end,
ranges:
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
, cb);
cb(start, end);
);
</script>
2 Answers
2
Do the "start" and "end" represent your dates?
If so: location.href += "&startDate=" + start + "&endDate=" + end;
Btw, this will actually reload the page with these parameters, plus those that already exists of course, unless it's some form of Hashtag URL.
P.s. Just be careful that you don't put this somewhere, where the code will call itself again upon page reload because then you could get stuck in an infinite reload loop until the URL buffer break.
store it in session using javascript
// Store
sessionStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = sessionStorage.getItem("lastname");
Check this tutorial to get more details
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.