IE javascript location, window.open - javascript

I have a small problem. For some weird reason any attempt to change url via javascript, be it window.open, window.location, window.location.href, etc. doesnt move to the desired page, but adds it to end of the url. It doesnt even matter what IE version, from 6-8
E.g.
http://localhost/blabla/produkt/philips-fc-861501-animal-care/3639
ends in
http://localhost/blabla/produkt/philips-fc-861501-animal-care/added-by-javascript
I have no idea why this happens...
On this page
http://localhost/blabla/objednat-tovar?step=deal-detail
it works as intended.
Any help is appreciated...
EDIT:
Some code.
I am on
http://localhost/blabla/produkt/philips-fc-861501-animal-care/3639
// code
test
function aaa(where) {
window.location = where;
}
Ends in
http://localhost/blabla/produkt/philips-fc-861501-animal-care/new_location
Same thing happens with window.location.href, window.open and only in IEs

I'm guessing that the browser tries to parse the location as a URL and if it fails then, presumably, it does whatever it wants (IE seems to append the string to the current location). For example:
window.location = 'about:blank'; // OK, since it's a valid pseudo-url.
window.location = 'foo'; // No effect, since this isn't a URL.
window.location = 'http://example.com/'; // OK, browse to that page.
window.location = 'bar'; // Depends on what the browser wants to do...

you don't need javascript: here
test
function aaa(where) {
window.location = where;
}

Related

window.location.href.replace does not replace the content of the url

I would like to redirect my current page to a page with similar URL except for one parameter.
Here is what I have tried
window.location = window.location.href.replace("trip-start="+/^\d{4}\-\d{2}\-\d{2}$/g, "trip-start="+this.value);
and also:
window.location.search = window.location.search.replace("trip-start="+/^\d{4}\-\d{2}\-\d{2}$/g, "trip-start="+this.value);
Unfortunately, the page is "redirected" (i.e refreshed) but the url stays exactly the same.
Am I missing something ?
Thanks you
[EDIT]
I investigated a bit, and actually the problem amount to this
let text = window.location.href;
let result = text.replace("trip-start="+/^\d{4}\-\d{2}\-\d{2}$/g, "trip-start=2000-00-00");
alert(result);
the "result" is supposed to be the URL with the parameter "trip-start" set to 2000-00-00, but again, nothing changes.
Please use history API
history.replace(newUrl)

Why changing `window.location.href` doesn't work as a return?

I would have expected, that in the following function the first window.location.href works as a return, gets redirected to example.com, and the rest of the code would be ignored.
() => {
window.location.href = 'http://example.com/'; // Does nothing
console.log('does it log?'); // Yes, it logs
window.location.href = 'http://example.org'; // Redirects here
}
Navigating to another page sounds like a definitive stop: discard DOM, abandon XHR queries, leave the site etc. Why the rest is still executed?
Short answer - it depends on the browser's speed of doing the redirect to the other page, hence unpredictable.
What's happening here is that the browser will try to execute the code after
window.location.href = 'http://example.com/';
Until the redirect occures and the page will go to the next web adress the rest of the code will execute as it supposed to run regular so the number of lines of code that will be executed actually depends on the browser's speed of doing the actual redirecting.

javascript page refresh in Safari

I am trying figure out how to refresh page in Safari (5.1) using javascript and nothing seems to work.
So far, I have tried,
window.location.href = window.location.href
window.location = window.location.href
window.location.reload(true)
window.location.replace(window.location.href)
What is the right way of handling page refresh in Safari?
Apparently, Safari on Mac or iOS has a bug with location.reload, so, I've developed this simple cross browser solution taking advantage of the url query string:
function refresh() {
var url = location.origin;
var pathname = location.pathname;
var hash = location.hash;
location = url + pathname + '?application_refresh=' + (Math.random() * 100000) + hash;
}
location.reload(true); // works for safari
If you didn't know already this site, let have a look on it, you will have a lot of example for refreshing page: http://www.quackit.com/javascript/javascript_refresh_page.cfm
You should always use the reload() method from the location object...
window.location.reload();
Set the first argument to true if you want to hard reload (send a new GET request for the page).

Firefox addon / javascript: get url from bar

I followed the example code from the addon dev site have successfully put a button onto FF :)
now i want to make that button do something interesting so I thought I would run an alert with the address that is currently in the bar... but this does not work:
CustomButton = {
1: function () {
alert("Just testing 1"+document.location.href);
},
}
except for the +document.location.href it's the exact demo code I got from the dev site...
You should note that in extension developing, document and window variables refers to the Host Browser not the browser that contains the web site.
you should use
gBrowser.selectedTab
for getting current tab and then using
currentURI.host
for getting URL host
also note that selectedTab returns a tab variable then you should get the window of that tab.
then the whole code will be:
gBrowser.getBrowserForTab(gBrowser.selectedTab).currentURI.host
Do you want to get the location of current document or the string that is in the location bar?
For location of current document
content.location.href
For the string in location bar
document.getElementById("urlbar").value
or
gURLBar.value
Those work for me, what context are you using it/how are you calling the function?
> document.location.href
< "http://stackoverflow.com/questions/6352035/firefox-addon-javascript-get-url-from-bar"
You can also use window.location.href
> window.location.href
< "http://stackoverflow.com/questions/6352035/firefox-addon-javascript-get-url-from-bar"
Try alert("The current page URL is " + browser.currentURI.spec) and see how that goes for you.
See also:
Firefox extension development : Get URL of new tab and https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIURI
Took a while to figure it all out:
gBrowser.currentURI.spec;
is how you do it.
Here's some code you may find useful
window.addEventListener('load', function (e) {
var href = gBrowser.currentURI.spec;
if ( href.match(/website.com/) ) {
var contentDoc = content.document;
// Do some stuff to the current website.com's DOM
}
}, true);

History.Back with refresh

I would like to have the History.back(); functionality with a complete refresh of previous page.
Any idea how to do that (and make it work in IE, FF and Chrome).
You could redirect (by window.location) to document.referrer
i.e.
window.location.href = document.referrer;
Internet Explorer fix for passing referrer to a particular location:
if(IE){ //IE, bool var, has to be defined
var newlocation = document.createElement('a');
newlocation.href = URLtoCall;
document.body.appendChild(newlocation);
newlocation.click();
}
You can also use the location replace() method:
window.location.replace(document.referrer)

Categories