What is the difference between document.location.href and document.location?
Is it the same across browsers?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
document.locationis a synonym forwindow.locationthat has been deprecated for almost as long as JavaScript has existed. Don’t use it.locationis a structured object, with properties corresponding to the parts of the URL.location.hrefis the whole URL in a single string. Assigning a string to either is defined to cause the same kind of navigation, so take your pick.I consider writing to
location.href = somethingto be marginally better as it’s slightly more explicit about what it’s doing. You generally want to avoid justlocation = somethingas it looks misleadingly like a variable assignment.window.location = somethingis fine though.