Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge

Richie Black
Richie Black
27,272 Points

Variable name "location" concatenated into doc.write result redirects page. Why?

Initially I had this: . var name = prompt('Give a name'); var action = prompt('Give an action'); var location = prompt('Give a location'); var story = name + ' went ' + action + ' in ' + location + '.';

alert('You have entered all information'); document.write(story); . This would redirect the page to .../(valueStoredInLocation) which gives a 404. When changing the variable name to "loc" it works. Why does this redirect?

1 Answer

Steven Parker
Steven Parker
231,007 Points

The name "location" is a shorhand for "Window.location", which is a special object that controls what page the browser is displaying. Assigning something to it is also a special shorthand for assigning it's "href"property. And doing that causes the browser to treat that value as a URL and attempt to load a page from it.

For more details, see the MDN page on Window.location.