var a = "hello world"(async function(){})()
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
–
–
–
–
Is there a difference between these two lines?
window.location = "http://www.google.com/";
window.location.replace("http://www.google.com/");
Short Answer
Background Facts
First, you want to know that:
window.location = "https://stackoverflow.com"
is an alias of window.location.href = "https://stackoverflow.com"
thus has the same functionality.
window.location:
Here, I am addressing window.location = "https://website.com"
in its context as window.location.href = "https://website.com"
The href property on the location object stores the URL of the current webpage.
On changing the href property, a user will be navigated to a new URL.
This adds an item to the history list
after moving to the next page, the user can click the "Back" button in
the browser to return to this page.
window.location.replace:
The replace function is used to navigate to a new URL without adding a record to the history.
This function is overwriting the topmost entry and replaces it from the history stack.
By clicking the "Back" button, you will not be able to return to the last page you visited before the redirect after moving to the next page.
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 while window.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 the href
property directly, this is faster by performance than using window.location.replace("http://www.google.com/");
because updating a function is slower than updating a property directly.
More about window.location
window.location
returns the Location
object that looks like this inside:
console.log(window.location);
// This is how the Location object that returns from window.location looks like
"ancestorOrigins": {},
"href": "https://stackoverflow.com/",
"origin": "https://stackoverflow.com",
"protocol": "https:",
"host": "stackoverflow.com",
"hostname": "stackoverflow.com",
"port": "",
"pathname": "/",
"search": "",
"hash": ""
The Location
object also has the following methods (functions):
Location.assign()
Loads the resource at the URL provided in parameter.
Location.reload()
Reloads the current URL, like the Refresh button.
Location.toString()
Returns a String containing the whole URL. It is a synonym for Location.href,
though, it can't be used to modify the value.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.