标签云

微信群

扫码加入我们

WeChat QR Code


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.

2018年05月23日33分18秒

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.

2018年05月23日33分18秒

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

2018年05月23日33分18秒

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?

2018年05月23日33分18秒

Socialz your argument is not valid as this question does specify the use of jQuery hence Javascript. If you want a PHP solution then go to the relevant PHP question. ps: disabling javascript is the most retarded thing a user can do.

2018年05月23日33分18秒

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

2018年05月23日33分18秒

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

2018年05月23日33分18秒

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

2018年05月23日33分18秒

NicolòMartini I posted a way of not losing REFERRER below if you want to check it out!

2018年05月23日33分18秒

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.

2018年05月24日33分18秒

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

2018年05月23日33分18秒

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?

2018年05月23日33分18秒

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.

2018年05月23日33分18秒

CoffeeAddict, 2 year old comment, but please school me on how location.href = 'http://stackoverflow.com'; is more verbose than the alternative in the answer here? Even if you take away the variable it is still more verbose and more characters to use jQuery for this.

2018年05月23日33分18秒

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

2018年05月23日33分18秒

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

2018年05月23日33分18秒

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

2018年05月24日33分18秒

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.

2018年05月23日33分18秒

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

2018年05月23日33分18秒

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.

2018年05月23日33分18秒

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?

2018年05月23日33分18秒

This answer is currently discussed at meta

2018年05月23日33分18秒

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.

2018年05月23日33分18秒

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

2018年05月23日33分18秒

even simpler: window.location = '/'

2018年05月23日33分18秒

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

2018年05月23日33分18秒

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.

2018年05月23日33分18秒

You don't need jQuery for redirect. No one needs jQuery for redirecting.

2018年05月23日33分18秒

But you don't need JQuery for anything, you can do everything without JQuery. The question was asking for JQuery.

2018年05月23日33分18秒

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.

2018年05月23日33分18秒

There's something called the right tool for the right job. Using jQuery to do redirects is like using the handle of a screwdriver to hammer a nail. You can do it but there are easier ways.

2018年05月23日33分18秒

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.

2018年05月23日33分18秒

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

2018年05月23日33分18秒

"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.

2018年05月23日33分18秒

But the question is asking for JQuery, which might abstract away future browser changes.

2018年05月23日33分18秒

what the point of wrapping everything in jquery?

2018年05月23日33分18秒

If jQuery is not nessesary, then all the better!

2018年05月23日33分18秒

But surely JQuery is never necessary? Isn't it built on top of normal javascript

2018年05月23日33分18秒

Well, yes, but jQuery can make things easier

2018年05月23日33分18秒

But the question is asking for JQuery

2018年05月23日33分18秒

You are using only javascript

2018年05月23日33分18秒

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

2018年05月23日33分18秒

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

2018年05月23日33分18秒

Again, the question is asking for JQuery

2018年05月23日33分18秒

ADS