I can use location.reload(true) to refresh the page and refetch the page. I want to redirect the user to a new URL and clear the cache.
More specifically, I have a "Clear all cookies" button that redirects the user to the homepage after being clicked. I want the homepage to be refreshed.
Do something like:
var loc = window.location.href; // or a new URL
window.location.href = loc + '?n=' + new Date().getTime(); // random number
EDIT: The only option for reloading static resources (see comment below) would be to append the random number to their URLs server-side by listening for the 'n' query string.
Sorry, but you can't clear the cache with Javascript.
Related
I'm trying to disable facebook's redirection to another country page by restructuring its URL.
An example would be - you're trying to open https://www.facebook.com/VisaID/ but if you're accessing the page from another country, you're going to be redirected to the closes page based on your geolocation (I guess). In my case, it's https://www.facebook.com/VisaCzechRepublic/?brand_redir=272936492828277 and I want to prevent this.
I tried to write a piece of code to solve this, however, when I try to reload the page with restructured URL it somehow adds extra window.location.hostname and window.location.pathname resulting in an absolute mess and breaking the page...
result = https://www.facebook.com/VisaCzechRepublic/www.facebook.com/272936492828277/?brand_redir=DISABLE
Code I used:
//Split URL to get FB ID
var brand = window.location.search;
brand = brand.split("=");
brandId = brand[1];
//Create new URL
//First facebook.com
//Second Page ID
//Third add ?brand_redir=DISABLE at the end
url = window.location.hostname + "/" + brandId + "/" + "?brand_redir=DISABLE";
//Open rearranged URL
window.open(url)
I expected Chrome to open a new URL exactly as is saved in the url being https://www.facebook.com/272936492828277/?brand_redir=DISABLE
but it somehow sneaks in extra https://www.facebook.com/VisaCzechRepublic/ and only after that ads the url I want.
Thanks a ton for any advice!
I am making a redirect using window.location.href to Listing view after an Item is added to DB after successful CREATE operation.
But what it does, it simply fetches old page (not showing the newly added item) that was previously in borwser's cache.
I want it to fetch every think from server, just like we use true parameter in document.location.reload(true) to load a fresh copy of page from server.
You can use this:
window.location.href = "http://www.mywebsite.com/products?t="+ (new Date().getTime());
Or:
window.location.href = "http://www.mywebsite.com/products?t="+ Math.random();
I need to get the previous url to redirect to the previous page. I have url like www.mysite.com/users/register/#1.
I use document.referrer to get the previous url,but it doesn't return hash part(#1). How to get the previous url including hash part?
How to get previous url including hash fragment using JavaScript?
As you've noted, the hash fragment part of that means you can't use document.referrer.
If the previous page was on the same origin: You'd need to have code on that page recording the full URL, for instance in sessionStorage.
On the previous page, perhaps each time hashChange is fired:
sessionStorage.setItem("last-url", location);
On the new page, to get the URL:
var lastUrl = sessionStorage.getItem("last-url");
If the previous page was on a different origin: I'm fairly certain you can't.
I need to get the previous url to redirect to the previous page.
Actually, you don't. You can just use history.go(-1) or history.back() to do that, which work regardless of the origin of the previous page.
try for previous url,
function backtopage() {
window.history.back();
}
May be you can use onhashchange event.
When url is changed,it produces a event with old url and new url.
The oldurl has even the hash part
$(window).bind('statechange',function(){
// Prepare Variables
var State = History.getState(),
url = State.url,
states = History.savedStates,
prevUrlIndex = states.length - 2,
prevUrl = states[prevUrlIndex].hash;
});
Try this one::
In previous page url:
www.mysite.com/users/register/#1
In Current Page:
$(document).ready(function() {
var referrerUrl = document.referrer.replace("#","e");
var correctUrl=referrerUrl.replace("e","#");
});
I'm trying to find a way in javascript to check which URL is loaded, then have a popup notifying the user to update their old bookmarket and have it redirect to the new location in a few seconds.
For example, the url maybe Http:\abc\myappage and I want to check if they are on the http:\abc site which if they are, the notification pops up and redirects them.
Currently I have a simple redirect to take them to the new site, but I never considered anyone that has an old bookmark which would never get updated if you don't inform them about the change.
Thanks.
You can access the current url from within JavaScript with window.location.
Using window.location you can access the current domain and path, then by setting window.location.href = 'your new site' after a few seconds or after some user interaction will cause the browser to navigate to the supplied url.
if(window.location.host === 'abc'){
alert('This url is no longer valid.');
window.location.href = 'http://abc/myappage
}
You can use window.location to get some information regarding the current url:
window.location.origin in the console on this current page, prints:
"http://stackoverflow.com"
Then you could run some JS logic to check against your other url and use alert() to crete the pop up.
working JSBIN: https://jsbin.com/gijola/edit?js,console
adding code:
function checker (url) {
var here = window.location.origin;
l(here);
if (here !== 'whatever you want to check') {
alert('please update your bookmark!!');
}
}
I was on Facebook and realised that when I change page the page address changes but the page does not redirect but loads via ajax instead.
You can tell because the console does not clear when you click the link but the URL changes.
Weird, but anyone know how it is done?
Facebook runs with massive AJAX calls that changes the page state and the sections.
So to make a page linkable to somebody by copying the URL address, every time you call an AJAX relevant function they updates the URL using a fake anchor "#!" plus the real address.
Simply when you load the real page (using F5 or linking that so somebody) a JS parser catchs the string after #! (if there is) and redirect you to baseaddress + that.
I belive something like this (untested):
var urlstr = new String(location.href);
var urlparm = urlstr.split('#!');
var last = urlparm.length - 1;
if( (urlparm[last] != urlparm[0]) && (urlparm[last] != "/") )
{ var redir = "http://www.facebook.com" + urlparm[last];
location.href = redir;
}
In Google Chrome instead the URL really changes, I'm according that there is an hash somewhere, but I don't know where and how.