window.location= 和 window.location.replace() 有什么区别?
这两条线有区别吗?
var url = "http://www.google.com/";
window.location = url;
window.location.replace(url);
Is there a difference between these two lines?
var url = "http://www.google.com/";
window.location = url;
window.location.replace(url);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
window.location
将一个项目添加到您的历史记录中,您可以(或应该能够)单击“后退”并返回到当前页面。window.location.replace
替换当前历史记录项,这样您就无法返回到它。请参阅
window.location
:哦,一般来说:
优于:
window.location
adds 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.replace
replaces the current history item so you can't go back to it.See
window.location
:Oh and generally speaking:
is favoured over:
太长了;
使用
location.href
或更好地使用window.location.href
;但是,如果您阅读本文,您将获得不可否认的证据。
事实是它很好用,但为什么要做一些有问题的事情呢?你应该走更高的道路,并按照可能应该做的方式去做。
这段代码在语法、逻辑、类型上都是完全正确的
你知道它唯一的问题吗?
它有
location
而不是location.href
那么
mystring
的值是多少?有人不做测试就真的知道吗?没有人知道这里到底会发生什么。天哪,我刚刚写了这个,但我什至不知道它是做什么的。location
是一个对象,但我分配一个字符串,它会传递字符串还是传递位置对象。假设对于如何实现这一点有一些答案。你能保证所有浏览器都会做同样的事情吗?我几乎可以猜测所有浏览器都会处理相同的情况。
如果你把它放入打字稿中,它会崩溃吗,因为类型编译器会说它应该是一个对象?
然而,这个对话比
location
对象要深入得多。这个转换是关于你想成为什么样的程序员?如果你走这条捷径,是的,今天可能会好,明天可能会好,见鬼,可能永远都好,但你先生现在是一个糟糕的程序员。这对你来说不会有什么好处,而且会让你失败。
将会有更多的对象。将会有新的语法。
你可能会定义一个只接受一个字符串但返回一个对象的 getter,最糟糕的是你会认为你正在做正确的事情,你可能会认为你很擅长这种聪明的方法,因为这里的人可耻地将你引入歧途。
对于 getter 和 setter,这段代码实际上可以工作,但仅仅因为它可以完成并不意味着这样做是“明智的”。
大多数编程人员都喜欢编程并且喜欢变得更好。在过去的几年里,我取得了很大的进步,也学到了很多东西。我现在知道的最重要的事情,尤其是当你编写库时,是一致性和可预测性。
做你能持续做的事情。
+"2"
<-- 这里将字符串解析为数字。你应该使用它吗?或者你应该使用
parseInt("2")
?var num ==+"2"
怎么样?从你所了解到的情况来看,从 stackoverflow 的角度来看,我不太抱有希望。
如果你开始遵循这两个词:一致且可预测。您将知道 stackoverflow 上大量问题的正确答案。
让我向您展示这将如何带来回报。
通常我将
;
放在我编写的每一行 javascript 上。我知道这样更有表现力。我知道这样更清楚了。我遵守了我的规则。有一天我决定不这样做。为什么?因为很多人告诉我不再需要它了,JavaScript 可以不需要它。所以我决定这样做。现在,因为我已经确信自己是一名程序员(因为你应该享受掌握一门语言的成果),所以我写了一些非常简单的东西,但我没有检查它。我删除了一个逗号,我认为我不需要重新测试删除一个逗号这样简单的事情。我在 es6 和 babel 中写了类似的东西
这段代码失败了并且花了很长时间才弄清楚。
由于某种原因,它所看到的内容隐藏
在源代码深处,它告诉我“hello world”不是一个函数。
为了更有趣,节点不显示转译代码的源映射。
浪费了那么多愚蠢的时间。我也向某人展示了 ES6 的出色之处,然后我必须开始调试并演示 ES6 是如何轻松且更好。没有说服力是吧。
我希望这能回答你的问题。这是一个老问题,更多的是对于仍在学习的下一代人来说。
当人们说无论哪种方式都没关系时提出疑问。一个更明智、更有经验的人很可能会告诉你其他明智的事情。
如果有人覆盖位置对象怎么办?他们将为旧版浏览器做一个垫片。它将获得一些需要调整的新功能,并且您的 3 年旧代码将失败。
我最后要思考的一点。
编写干净、清晰、有目的的代码对你的代码所做的事情是无法用正确或错误来回答的。它的作用是使您的代码成为推动者。
您可以使用更多插件、库,而不必担心代码之间的中断。
记录在案。使用
window.location.href
TLDR;
use
location.href
or better usewindow.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.
This code is perfectly correct syntax-wise, logic wise, type-wise
you know the only thing wrong with it?
it has
location
instead oflocation.href
what about this
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.location
is 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?This i can pretty much guess all browsers will handle the same.
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
location
object however. What this conversion is about what kind of programmer you want to be?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.
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.
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")
?what about
var num =+"2"
?From what you have learn, from the minds of stackoverflow i am not too hopefully.
If you start following these 2 words consistent and predictable. You will know the right answer to a ton of questions on 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.I wrote something similar to this in es6 and babel
This code fail and took forever to figure out.
For some reason what it saw was
hidden deep within the source code it was telling me "hello world" is not a function.
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.
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.
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 = "https://stackoverflow.com"
是window.location.href = "https://stackoverflow.com" 的别名。 com"
因此具有相同的功能。window.location
VSwindow.location.replace
window.location:
在这里,我在其上下文中处理
window.location = "https://website.com"
如window.location.href = "https://website.com"
浏览器返回此页面。
window.location.replace:
结论:
回答问题:
是的,我们的两个主题之间存在差异,主要是
window.location
使您能够在浏览器历史记录中返回window.location.replace()
不允许您返回浏览器历史记录,从而从浏览器历史记录中删除以前的 URL 记录。奖励:哪个更快?
当您使用以下内容时:
window.location = "http://www.google.com/";
您将直接更新href
属性,这在性能上会更快比使用window.location.replace("http://www.google.com/");
因为更新函数比直接更新属性慢。有关
window.location
的更多信息window.location
返回Location
对象,其内部如下所示:Location
对象还具有以下方法(函数):Origins & a Solution
The Question
Short Answer
Yes.
Background Facts
First, you want to know that:
window.location = "https://stackoverflow.com"
is an alias ofwindow.location.href = "https://stackoverflow.com"
thus has the same functionality.window.location
VSwindow.location.replace
window.location:
Here, I am addressing
window.location = "https://website.com"
in its context aswindow.location.href = "https://website.com"
the browser to return to this page.
window.location.replace:
Conclusion:
To answer the question:
Yes, there is a difference between our 2 subjects and mostly in the fact that
window.location
enables you to go back in the browser history whilewindow.location.replace()
doesn't let you go back in browser history, thus removing the previous URL record from the browser history.Bonus: Which is faster?
When you are using this:
window.location = "http://www.google.com/";
you are updating thehref
property directly, this is faster by performance than usingwindow.location.replace("http://www.google.com/");
because updating a function is slower than updating a property directly.More about
window.location
window.location
returns theLocation
object that looks like this inside:The
Location
object also has the following methods (functions):