Why isn't this javascript redirecting? - javascript

I want to have the user click on a button and redirect him/her to a different site I have used the window.location before but for some reason it's not working this time. I'm sure the selector is correct as if I change window.location to alert it alerts every time, so I can't figure what I'm doing wrong.
$('#show').click(function(){
if($('select[name="customers"]').val() == '')
window.location.replace = 'http://google.com/';
});

You have to use either
window.location.replace("http://google.com"); // This is like http redirect
or
window.location.href = "http://google.com"; // This is like a link
You can't do window.location.replace= "http://google.com";

its a method, not a property.
window.location.replace('http://google.com/');

Shouldn't that be windows.location.href = ...?

Set just window.location.href. Like this:
window.location.href = 'http://google.com/';

try: window.location = 'http://google.com'

Related

Refresh exact URL by JavaScript or jQuery

I am looking for a way to refresh the exact URL after ajax success. I try the following way:
location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
But in fact, I have another problem, the URL is https://example.com/electronic/?product_cat=mobile but after using the mentioned code, the URL will change and the URL will be https://example.com/electronic/
is there any way to keep ?product_cat=mobile after refreshing?
The easiest way of doing this will be
Get the current URL.
Redirect to it.
<script>
var current_url = window.location.href;
window.location.href = current_url;
</script>

Redirect to url with some addition in the url

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

Difference between window.location and href?

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

Javascript, redirect to a link retrieved by a function?

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();

How to redirect to home page

How can I redirect a user to home page?
Example: mywebsite.example/ddfdf/fdfdsf and I want to redirect to mywebsite.example
However I want to do it without typing the static name. How can I do this?
document.location.href="/";
document.location.href="/";
or
window.location.href = "/";
According to the W3C, they are the same. In reality, for cross browser safety, you should use window.location rather than document.location.
See: http://www.w3.org/TR/Window/#window-location
(Note: I copied the difference explanation above, from this question.)
window.location.href = "/";
This worked for me.
If you have multiple folders/directories, you can use this:
window.location.href = "/folder_name/";
Can you do this on the server, using Apache's mod_rewrite for example? If not, you can use the window.location.replace method to erase the current URL from the back/forward history (to not break the back button) and go to the root of the web site:
window.location.replace('/');
maybe
var re = /^https?:\/\/[^/]+/i;
window.location.href = re.exec(window.location.href)[0];
is what you're looking for?
window.location = '/';
Should usually do the trick, but it depends on your sites directories. This will work for your example
strRetMsg ="<script>window.location.href = '../Other/Home.htm';</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", strRetMsg,false);
Put this code in Page Load.
var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;
See this answer
https://stackoverflow.com/a/42291014/3901511
var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;

Categories