标签云

微信群

扫码加入我们

WeChat QR Code

How can I redirect the user from one page to another using jQuery or pure JavaScript?


I keep seeing both window.location = url; and window.location.href = url; How are they different? Are they? BTW, I didn't know about window.location.replace(url). Nice.

2019年06月26日46分45秒

window.location is the same as window.location.href, in terms of behavior. window.location returns an object. If .href is not set, window.location defaults to change the parameter .href. Conclude: Use either one is fine.

2019年06月26日46分45秒

var url = "website name" $(location).attr('href',url);

2019年06月26日46分45秒

MadScientist That is a backwards way to do it. The location object is not a an HTML element and using the jquery object to set it just seems wrong. Why would you use that when the straight JS code is so simple?

2019年06月26日46分45秒

Why do you want to redirect ? The login form should not be submitted? And even if there is no logic in the target page, you still will be able to submit the form to another page.

2019年06月26日46分45秒

in case of submit button add return false ; also inside your function

2019年06月26日46分45秒

The question is about javascript specifically, but it may be worth noting that a meta refresh can be used as a fail back in case the user has javascript disabled

2019年06月26日46分45秒

NicolòMartini If(IE) document.write("");

2019年06月26日46分45秒

If you're already using jQuery, just use $(location).attr('href',url);. window.location.href seems to have inconstant behavior in some browsers, in fact, it flat out doesn't work in my version of Firefox. I've heard tell of setting window.location directly not working in versions of IE.

2019年06月26日46分45秒

"is better" should be removed. As stated later in the answer, the best solution is situational. OP does not ask "how can I emulate an HTTP redirect with Javascript..."

2019年06月26日46分45秒

This is that literal answer to the question. If you are already using Jquery and therefore have it already loaded then it will be more bandwidth efficient and of course clearer to use the shortcut

2019年06月26日46分45秒

More importantly, is there a way to do this with jQuery that is absracted?This is just a wrapper for window.location.href = url; But if jQuery had some function that, if window.location.href = url; wasn't going to work in the current environment (browser, OS, etc.) jQuery core could compensate?

2019年06月26日46分45秒

Forcing jQuery into the equation in this way is just ridiculous and pointless, especially since window.location is not an element and therefore does not have attributes.

2019年06月26日46分45秒

I somehow doubt this still works with recent jQuery versions where .attr() actually sets attributes (i.e. .setAttribute())

2019年06月26日46分45秒

deltaray This is not another way to redirect, like said above, it's a meaningless wrapper around the location object, which, is not even an element! This reminds me of i.stack.imgur.com/ssRUr.gif

2019年06月26日46分45秒

Foot note: checking for referrer as a security measure is a lousy solution. duckduckgo.com/?q=referrer+spoofing

2019年06月26日46分45秒

If you have an HTTP GET session ID in the referrer URL it can be used to check that against the session for validity.

2019年06月26日46分45秒

mcpDESIGNS are you sure that you've tried to use location.assign? I use in my app with IE8, and I don't lose the HTTP_REFERER header.

2019年06月26日46分45秒

At least back in 2012 it wasn't working, maybe later patches of IE8 fixed it - that's good to hear though!

2019年06月25日46分45秒

Your mention of HTTP_REFERER helped me understand the problem I'm having with window.open and I found a solution here: stackoverflow.com/questions/7580613/…. By the way I'm having the problem in IE11!

2019年06月26日46分45秒

window.navigate is old-IE-only (Firefox/Chrome do not support this). If you want to enumerate all options, don't forget about document.location.

2019年06月26日46分45秒

This does not explain anything at all. What is someone supposed to pick? Pick one randomly from the list? Also, what does this add to the existing answers?

2019年06月26日46分45秒

document.location = document.referrer;Please add it in list also. This does not work like History.back rather makes the page Refresh again.

2019年06月26日46分45秒

LéoLam That's why there is a search engine called Google. :-) You can search each each one on Google and find the difference. You don't want to pick one randomly.

1970年01月01日00分02秒

IamtheMostStupidPerson exactly my point. The answer is useless.

2019年06月26日46分45秒

You could also do document.location.replace(redirectURL) if you don't want the first page to be in the browser history

2019年06月26日46分45秒

window.location.replace(window.location.protocol + "//" + window.location.host)

2019年06月26日46分45秒

even simpler:window.location = '/'

2019年06月26日46分45秒

Iftah - that is dependant on the "base" meta tag.

2019年06月26日46分45秒

I recommend using assign() over href= too as the latter did not work properly for me in some cases.

2019年06月26日46分45秒

It maybe works, but I am not sure if this the right way to do it. I'm not see documentation that support this. It could leads programmers to a bad programming practices.

2019年06月25日46分45秒

I address his request for jQuery by saying it's not needed. JQuery has a lot of great useful shortcuts for JavaScript but there is no need for any shortcuts for redirects. JQuery is just JavaScript. Nothing more. The normal way to call a redirect will work with his functions using jQuery.

2019年06月26日46分45秒

Patrick, by your own logic there is no need for JQuery in all situations. The whole point is convenience, and the question asked for it. The best tool I'd say is the JQuery version because it will probably abstract away browser specifics for the future. So your argument is still null and void. The question asked for JQuery.

2019年06月26日46分45秒

You may be right about that currently, however you are not able to see the future and cannot at this time predict how future proof it is. And the fact remains the question asked for JQuery!

2019年06月26日46分45秒

"Yeah JavaScript could change and if it changes then jQuery then needs to change as jQuery is JavaScript. –Patrick W. McMahon yesterday" Thankyou, I rest my case.

2019年06月26日46分45秒

You have no case. JQuery has a higher chance to change then JavaScript. New version of JQuery get realest all the time and old jQuery becomes obsolete and them makes problems will code reliant on a set version of JQuery.

2019年06月26日46分45秒

JQuery is arguably a better idea as it could abstract away future browser changes/deprecations

2019年06月26日46分45秒

"Using jQuery" is um, inaccurate. And the example of using a complete URL won't work, as that's not a complete URL. There's no protocol, so it'll be interpreted as a relative URL.

2019年06月26日46分45秒

what the point of wrapping everything in jquery?

2019年06月26日46分45秒

It is preferable not to write in caps, although good answer

2019年06月26日46分45秒

Is there a reason for storing the url in jQuery as a variable, but not with Vanilla?

2019年06月26日46分45秒

better do this: setTimeout(function() {window.location.href = "google.com"}, delay);

2019年06月26日46分45秒