views:

107

answers:

4
self.location = 'cache:' + self.location

I want to redirect from "[URL]" to "cache:[URL]". I just want this code to work in Chrome browser.

+2  A: 

isn't that rather:

window.location

you will need to do user agent string checking, if it should work only for chrome.

dusoft
+2  A: 

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.

Guffa
I don't think this solves his issue from a quick test in Chrome. The code is valid javascript, but it doesn't actually redirect the page, due to the invalid protocol.
Jeff B
@Jeff: Well, it's rather because it's not an URL at all, but a google search.
Guffa
A: 

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.

Daniel Matthews
This code doesn't work...
Sergey
+3  A: 

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
Jeff B