I'm trying to get the parameters of my search criteria into the url so that it's possible to copy and paste the specific search.
Currently I have this:
// Search criteria in the url
var ready = function(e){
$("#filterrific_filter").click(function(){
$.get($("#filterrific_filter").attr("action"), $("#filterrific_filter").serialize(), null, "script");
history.pushState(null, document.title, $("#filterrific_filter").attr("action") + "?" + $("#filterrific_filter").serialize());
e.preventDefault();
});
$(window).bind("popstate", function() {
$.getScript(location.href);
});
};
$(document).ready(ready);
$(document).on('page:load', ready);
The problem is: when a result is clicked on, and the users tries to go back to the search page again, they don't get back, they stay on the same page. The url changes though.
Here is a video to display the problem
And here is a link to the webpage, if you would like to test it yourself
Thanks in advance!
Try removing this whole section:
$(window).bind("popstate", function() {
$.getScript(location.href);
});
Since your apartments#show action (a guess, assuming you're using a RESTful design in your Rails backend) is a round-trip request (not AJAX, like your filter), and your query string URL will run the filterrific scripts correctly on a reload, you might as well let the back button work the default way.
Disclaimer: this is a shot in the dark without more context. If it doesn't work for you, and you provide more context, I'll happily revisit.
I have been trying to ask user before leaving the page a confirmation in knockout.js using before callback of sammy.js in my case for a specific URL here is the code what i wrote.
router.sammy = Sammy(function () {
this.before('#view/?:viewThingObj?/edit', function() {
if(!window.confirm('Are you sure you want to leave this page?')) {
return false;
}
});
this.before(/.*/, function () {
}
});
but it doesn't seems to be working is there something wrong? is this because of the route i added for all i.e. /.*/
My answer here should do what you're trying to do. My group had to abandon before because it still changed the URL prior to attempting to run the route.
I'm using a code like this to create an account and than i want to validate (with some specifics functions) and after that redirect to the main page.
$scope.createAccount = function(cad) {
$http.post('dist/func.php?action=createAccount', cad).then(
function(res) {
//..more functions
$window.location.href = '/adm';
},
function(err) {
Notification.error(feedbackError);
}
);
};
The problem is, when the user arrive in the new page, the mainCtrl is not running (the page is not fully reloading) so i can't get essential data for this new user.
I saw some other similar questions, but all of them says we should use $window.location.href instead of $location to get a full page reload. But until now, i had no success with it.
How can i solve it?
I've a probleme in my code. The aim is to complete a simple form, then you click on a submit button. It do an Ajax resquest to go in the method. On success in the ajax request, i use windows.history.back() to go to the previous page ans here i want to refresh this page, to refresh values which are modificated by the form ! Have you an idea about that ?
$('#form_edit').submit(function (e)
{
e.preventDefault();
$.ajax({
url: $('#form_edit').attr('action'),
type: 'POST',
cache: false,
data: $(this).serialize(),
success: function (data) {
if (data === true) {
alert("Modification réussie !");
window.history.back();
location.reload(); <= on success i want to refresh previous page
}
else {
alert("Modification échouée !");
}
},
error: function ()
{
alert("Modification échouée !");
}
})
})
window.history.back(); Sometimes it's an issue with javascript compatibility with ajax call or design-related challenges.
I would use this below function for go back with the refresh.
function GoBackWithRefresh(event) {
if ('referrer' in document) {
window.location = document.referrer;
/* OR */
//location.replace(document.referrer);
} else {
window.history.back();
}
}
In your html, use:
BACK`
For more customization you can use history.js plugins.
This is the correct answer. It will refresh the previous page.
window.location=document.referrer;
It will have already gone back before it executes the reload.
You would be better off to replace:
window.history.back();
location.reload();
with:
window.location.replace("pagehere.html");
Try these ...
Option1
window.location=document.referrer;
Option2
window.location.reload(history.back());
You can't do window.history.back(); and location.reload(); in the same function.
window.history.back() breaks the javascript flow and redirects to previous page, location.reload() is never processed.
location.reload() has to be called on the page you redirect to when using window.history.back().
I would used an url to redirect instead of history.back, that gives you both a redirect and refresh.
I know this post is old but this can help.
window.location.replace(document.referrer);
After struggling with this for a few days, it turns out that you can't do a window.location.reload() after a window.history.go(-2), because the code stops running after the window.history.go(-2). Also the html spec basically views a history.go(-2) to the the same as hitting the back button and should retrieve the page as it was instead of as it now may be. There was some talk of setting caching headers in the webserver to turn off caching but I did not want to do this.
The solution for me was to use session storage to set a flag in the browser with sessionStorage.setItem('refresh', 'true'); Then in the "theme" or the next page that needs to be refreshed do:
if (sessionStorage.getItem("refresh") == "true") {
sessionStorage.removeItem("refresh"); window.location.reload()
}
So basically tell it to reload in the sessionStorage then check for that at the top of the page that needs to be reloaded.
Hope this helps someone with this bit of frustration.
I know this is pretty old but I thought my answer would help someone as most of codes here refreshed current page not previous one
so you can't get back to previous page and refresh it at same time as JS will run the code that takes you back and stops (will not run refresh page part) so I found a way to combine both functions
window.location.assign(window.history.back());
This basically will load a "new page" (so it will refresh it) but at same time it will load the previous page in browser history
window.history.back() does not support reload or refresh of the page. But you can use following if you are okay with an extra refresh
window.history.back()
window.location.reload()
However a real complete solution would be as follows:
I wrote a service to keep track of previous page and then navigate to that page with reload:true
Here is how i did it.
'use strict';
angular.module('tryme5App')
.factory('RouterTracker', function RouterTracker($rootScope) {
var routeHistory = [];
var service = {
getRouteHistory: getRouteHistory
};
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
routeHistory = [];
routeHistory.push({route: from, routeParams: fromParams});
});
function getRouteHistory() {
return routeHistory;
}
return service;
});
Make sure you have included this js file from you index.html
<script src="scripts/components/util/route.service.js"></script>
Now from you stateprovider or controller you can access this service and navigate
var routeHistory = RouterTracker.getRouteHistory();
console.log(routeHistory[0].route.name)
$state.go(routeHistory[0].route.name, null, { reload: true });
or alternatively even perform checks and conditional routing
var routeHistory = RouterTracker.getRouteHistory();
console.log(routeHistory[0].route.name)
if(routeHistory[0].route.name == 'seat') {
$state.go('seat', null, { reload: true });
} else {
window.history.back()
}
Make sure you have added RouterTracker as an argument in your function
in my case it was :
.state('seat.new', {
parent: 'seat',
url: '/new',
data: {
authorities: ['ROLE_USER'],
},
onEnter: ['$stateParams', '$state', '$uibModal', 'RouterTracker', function($stateParams, $state, $uibModal, RouterTracker) {
$uibModal.open({
//....Open dialog.....
}).result.then(function(result) {
var routeHistory = RouterTracker.getRouteHistory();
console.log(routeHistory[0].route.name)
$state.go(routeHistory[0].route.name, null, { reload: true });
}, function() {
$state.go('^');
})
Brandon Hoult answered of Dec 18, 2019 at 3:33
I didn't get at at first but YES, it works! via session variable flag, but I'll say it it backwards
Set this in the page you want to be refreshed if user goes back to it with history back or browser back.
if (sessionStorage.getItem("refresh") == "true") {
sessionStorage.removeItem("refresh"); window.location.reload()
}
Set this flag in the page you make changes like the shopping cart or the ajax mentioned that will change stuff, some setttings, classes, etc.
sessionStorage.setItem('refresh', 'true');
My case:
I hard code settings in a buy page while loading it, like prices and class of buy buttons: "add" or "in cart" icons. User adds an item to cart, then I add the product calling ajax and change that button's class to "in cart". If user wants to remove it from cart just clicks the cart icon and ajax again to remove it from cart and change class to "add".
Problem:
User goes to cart page itself, see the products and decide to go back and page show old buttons icons because is history (first loaded page, not changed one) so need to reload the show updated info.
Solution above does:
Buy page will reload if user goes to cart and back since cart page set a session variable "refresh" and buy page checks if "refresh" = true, set refresh it to false (so only cart page can set refresh to true and refresh the page.
Session variable "refresh" is a name so you can call it whatever else so you can use as many different flags as you want.
step 1: save the referrer URL in the local cache on load functions.
$(function () {
let refUrl = document.referrer;
let origin = location.origin;
if (refUrl.replace(origin, '') !== location.pathname) {
localStorage.setItem("history", refUrl.replace(origin, ''));
}
});
step 2: redirect the page to the referrer URL on link click.
$('body').on('click', '.lnkRurl', function () {
location.href = localStorage.getItem('history');
});
This is similar to, but not the same as How can I refresh a page with jQuery?:
I bring up a modal form that collects some stuff from the user and passes it off to the server via a $.ajax() call. The server sends back a path that should become the new window.location of the browser. So the ajax call wants to be something like:
$.ajax({
// stuff
success: function (destination) {
// other stuff
window.location = destination;
}),
// still more stuff
});
This works fine as long as destination is a pure path, like /some_path and if the browser is not currently on that page. However, if the path is the page that I'm currently on and also includes a target -- /some_path#some_target, I lose: the browser simply repositions the page at the specified target, but does not hit the server for a fresh view of the page, which I need (since the server has done some stuff during the ajax call).
So, maybe I just add a location.reload() after the window.location call? That would work when the code is running on the page to which it's being returned, I think. But if I'm on another page, I get hit by a race condition, where the reload is called before the browser has finished making the window.location change, and I get the old page reloaded, not the new destination.
Blurgh. Is there any way around this?
One approach would be to check if window.location.pathname (which is the path without # or ?) is the same as destination within your success callback:
success: function (destination) {
// other stuff
if (destination === window.location.pathname) {
window.location.reload(); // reload if we are on the same page
} else {
window.location = destination; // otherwise, navigate to "other" page
}
}),
window.location.reload() reloads the current page with POST data, while window.location.href=window.location.href does not include the POST data.