Redirect to cache URL in Chrome browser
self.location = 'cache:' + self.location
I want to redirect from "[URL]"开发者_如何学Python to "cache:[URL]". I just want this code to work in Chrome browser.
I don't think that's actually a legal location. Chrome intercepts this from the address bar as a search, and sends it to google.
You are better off doing this:
window.location.href = 'http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cache:' + window.location
isn't that rather:
window.location
you will need to do user agent string checking, if it should work only for chrome.
Use the window object directly, and set the href property instead of the entire location object.
window.location.href = 'cache:' + window.location.href;
Edit:
I did some testing to see what Chrome actually did with this URL. You can't redirect to an URL like that, as what you are trying to use is not actually an URL. It's a google search, that you can type in the address field of the chrome browser, but you can't do the same by setting the location of the window. By setting the location you go past the step where the browser checks if it's an URL or a search that is typed in.
Could be an odd behaviour of self.location, as i've always used window.location.
Try concatenating BEFORE redirecting the window too, like so:
var currentLocation = window.location;
var newLocation = 'cache:'+currentLocation;
window.location = newLocation;
That should work.
精彩评论