What's the difference between this word and action?
<script>
window.location.href = 'index.php';
window.location = 'index.php';
</script>
because some instances that after clicking the button there is a milisecs thats see the way of changing the page..what is that?
window.location.href returns the location of the current page while window.location returns a Location object, which contains information about the URL of the document and provides methods for changing that URL. You can also assign to this property to load another URL.
Check here
Related
I have a select item with bunch of cities on my website. When the visitor selects some city this happens:
$("#city-selector").change(function() {
var url = $(this).val(); // get selected value
if (url) { // require a URL
document.location.href = url; // redirect
}
});
Each option inside the select has a value parameter that contains subdomain url in it.
The problem is document.location.href doesn't behave like a simple link. It clears visitor id and it looks like the visitor doesn't have a referer and came to the new subdomain out of nowhere. Is there a problem with functions I use or should I dig into crossdomain sessions/cookies? How do I make it behave properly?
Use window.location.href. This is similar when clicking a link
window.location.href = url;
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!!');
}
}
Suppose, i've my current url:
http://localhost:3000/contests
after the button press, i am going to redirect to the following url:
http://localhost:3000/contests?keywords=algo
My problem is, i want to redirect to the path with other additional information like:
http://localhost:3000/contests?keywords=algo&show=present
here, the &show=present is added to the redirected url. How can i do that using javascript?
How can i get the redirect url using javascript?
Do this in your button click.
var url=document.URL + "&show=present";
window.location.href = url;
Use document.URL to get the current url.
Hope this will help you!!
location.href = location.href + "?keywords=algo"
also check all the other possibilities with window.location object at MDN
Right now I'm showing an href that redirects to a certain location, I want to redirect automatically to the same link but don't know how.
This is the link:
<script>
document.write(' Go ');
</script>
I tried with window.location but I'm messing somehow since I go literally to the name of the function.
How should I do it? Thanks
Setting window.location.href should redirect you to a new page, like this:
var url = geoip_city(); // url should be a valid url string
window.location.href = url;
You should use window.location.href to redirect. So assuming you have a function which returns an url:
function geoip_city() {
...
return 'http://example.com?foo=bar';
}
you could redirect like this:
window.location.href = geoip_city();