Javascript window.location= 和 window.location.replace() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1865837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between window.location= and window.location.replace()?
提问by Aaron Digulla
Is there a difference between these two lines?
这两条线有区别吗?
var url = "http://www.google.com/";
window.location = url;
window.location.replace(url);
回答by cletus
window.locationadds an item to your history in that you can (or should be able to) click "Back" and go back to the current page.
window.location在您的历史记录中添加一个项目,您可以(或应该能够)单击“返回”并返回当前页面。
window.location.replacereplaces the current history item so you can't go back to it.
window.location.replace替换当前历史记录项,因此您无法返回。
See window.location:
assign(url): Load the document at the provided URL.
replace(url):Replace the current document with the one at the provided URL. The difference from theassign()method is that after usingreplace()the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
assign(url):在提供的 URL 加载文档。
replace(url):用提供的 URL 中的文档替换当前文档。与该assign()方法不同的 是,使用replace()当前页面后 不会保存在会话历史记录中,这意味着用户将无法使用后退按钮导航到它。
Oh and generally speaking:
哦,一般来说:
window.location.href = url;
is favoured over:
优先于:
window.location = url;
回答by Lpc_dark
TLDR;
TLDR;
use location.hrefor better use window.location.href;
使用location.href或更好地使用window.location.href;
However if you read this you will gain undeniable proof.
但是,如果您阅读本文,您将获得不可否认的证据。
The truth is it's fine to use but why do things that are questionable. You should take the higher road and just do it the way that it probably should be done.
事实是它很好用,但为什么要做有问题的事情。你应该走更高的路,按照可能应该做的方式去做。
location = "#/mypath/otherside"
var sections = location.split('/')
This code is perfectly correct syntax-wise, logic wise, type-wise you know the only thing wrong with it?
这段代码在语法、逻辑、类型方面都是完全正确的,你知道它唯一的错误吗?
it has locationinstead of location.href
它有location而不是location.href
what about this
那这个呢
var mystring = location = "#/some/spa/route"
what is the value of mystring? does anyone really know without doing some test. No one knows what exactly will happen here. Hell I just wrote this and I don't even know what it does. locationis an object but I am assigning a string will it pass the string or pass the location object. Lets say there is some answer to how this should be implemented. Can you guarantee all browsers will do the same thing?
的价值是mystring什么?有没有人真的不做一些测试就知道了。没有人知道这里究竟会发生什么。见鬼,我刚刚写了这个,我什至不知道它是做什么的。location是一个对象,但我正在分配一个字符串,它将传递字符串还是传递位置对象。让我们说应该如何实施有一些答案。你能保证所有浏览器都会做同样的事情吗?
This i can pretty much guess all browsers will handle the same.
我几乎可以猜测所有浏览器都会处理相同的问题。
var mystring = location.href = "#/some/spa/route"
What about if you place this into typescript will it break because the type compiler will say this is suppose to be an object?
如果你把它放到打字稿中会怎样,因为类型编译器会说这是一个对象?
This conversation is so much deeper than just the locationobject however. What this conversion is about what kind of programmer you want to be?
然而,这次谈话比location对象更深刻。这种转换是关于你想成为什么样的程序员?
If you take this short-cut, yea it might be okay today, ye it might be okay tomorrow, hell it might be okay forever, but you sir are now a bad programmer. It won't be okay for you and it will fail you.
如果你走这条捷径,是的,今天可能没问题,明天可能没问题,地狱可能永远没问题,但你先生现在是一个糟糕的程序员。这对你来说不好,它会让你失望。
There will be more objects. There will be new syntax.
会有更多的对象。会有新的语法。
You might define a getter that takes only a string but returns an object and the worst part is you will think you are doing something correct, you might think you are brilliant for this clever method because people here have shamefully led you astray.
你可以定义一个只接受一个字符串但返回一个对象的 getter,最糟糕的是你会认为你做的事情是正确的,你可能认为你对这种聪明的方法很聪明,因为这里的人可耻地把你引入歧途。
var Person.name = {first:"John":last:"Doe"}
console.log(Person.name) // "John Doe"
With getters and setters this code would actually work, but just because it can be done doesn't mean it's 'WISE' to do so.
使用 getter 和 setter,这段代码实际上可以工作,但仅仅因为它可以完成并不意味着这样做是“明智的”。
Most people who are programming love to program and love to get better. Over the last few years I have gotten quite good and learn a lot. The most important thing I know now especially when you write Libraries is consistency and predictability.
大多数编程人员都喜欢编程并喜欢变得更好。在过去的几年里,我变得很好,学到了很多东西。我现在知道的最重要的事情是一致性和可预测性,尤其是在编写库时。
Do the things that you can consistently do.
做你可以持续做的事情。
+"2"<-- this right here parses the string to a number. should you use it?
or should you use parseInt("2")?
+"2"<-- 此处将字符串解析为数字。你应该使用它吗?还是应该使用parseInt("2")?
what about var num =+"2"?
怎么样var num =+"2"?
From what you have learn, from the minds of stackoverflow i am not too hopefully.
从你所学到的,从 stackoverflow 的想法来看,我并不抱太大希望。
If you start following these 2 words consistent and predictable. You will know the right answer to a ton of questions on stackoverflow.
如果您开始遵循这两个词一致且可预测。您将知道有关 stackoverflow 的大量问题的正确答案。
Let me show you how this pays off.
Normally I place ;on every line of javascript i write. I know it's more expressive. I know it's more clear. I have followed my rules. One day i decided not to. Why? Because so many people are telling me that it is not needed anymore and JavaScript can do without it. So what i decided to do this. Now because I have become sure of my self as a programmer (as you should enjoy the fruit of mastering a language) i wrote something very simple and i didn't check it. I erased one comma and I didn't think I needed to re-test for such a simple thing as removing one comma.
让我告诉你这是如何获得回报的。通常我会放在;我写的每一行 javascript 上。我知道它更具表现力。我知道这更清楚。我遵守了我的规则。有一天我决定不这样做。为什么?因为很多人告诉我不再需要它,而 JavaScript 可以不用它。所以我决定这样做。现在因为我已经确定自己是一名程序员(因为你应该享受掌握一门语言的成果)我写了一些非常简单的东西,我没有检查它。我删除了一个逗号,我认为我不需要重新测试删除一个逗号这样简单的事情。
I wrote something similar to this in es6 and babel
我在 es6 和 babel 中写了类似的东西
var a = "hello world"
(async function(){
//do work
})()
This code fail and took forever to figure out. For some reason what it saw was
这段代码失败了,花了很长时间才弄明白。出于某种原因,它看到的是
var a = "hello world"(async function(){})()
hidden deep within the source code it was telling me "hello world" is not a function.
隐藏在源代码深处,它告诉我“hello world”不是函数。
For more fun node doesn't show the source maps of transpiled code.
为了更有趣,节点不显示转译代码的源映射。
Wasted so much stupid time. I was presenting to someone as well about how ES6 is brilliant and then I had to start debugging and demonstrate how headache free and better ES6 is. Not convincing is it.
浪费了这么多愚蠢的时间。我也在向某人介绍 ES6 是多么出色,然后我不得不开始调试并演示 ES6 是多么令人头疼和更好。没有说服力是吧。
I hope this answered your question. This being an old question it's more for the future generation, people who are still learning.
我希望这回答了你的问题。这是一个古老的问题,它更适合未来的一代,那些仍在学习的人。
Question when people say it doesn't matter either way works. Chances are a wiser more experienced person will tell you other wise.
当人们说无论哪种方式都无关紧要时的问题。机会是一个更聪明更有经验的人会告诉你其他明智的。
what if someone overwrite the location object. They will do a shim for older browsers. It will get some new feature that needs to be shimmed and your 3 year old code will fail.
如果有人覆盖位置对象怎么办。他们将为旧浏览器做一个垫片。它将获得一些需要填充的新功能,并且您的 3 年旧代码将失败。
My last note to ponder upon.
我要思考的最后一个笔记。
Writing clean, clear purposeful code does something for your code that can't be answer with right or wrong. What it does is it make your code an enabler.
编写干净、清晰、有目的的代码可以为您的代码做一些无法回答对错的事情。它的作用是让您的代码成为推动者。
You can use more things plugins, Libraries with out fear of interruption between the codes.
你可以使用更多的东西插件,库而不用担心代码之间的中断。
for the record. use
作为记录。用
window.location.href
window.location.href

