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 trialDee K
17,815 PointsQuestions arising from the conditional statement in this video.
The conditional code in this video made me think about browser functionality.
if ($anchor.parent().hasClass('selected')) {
$option.prop('selected', true);
}
How does the web browser intelligently know which page it is on?
I understand this code is essentially saying if the parent element (list item) has a class of selected, set the selected property in $option to true.
But my question is HOW does the browser know which page it is on? We have 6 html docs, yet the browser can understand which page it is on?
Thanks
1 Answer
Joel Bardsley
31,249 PointsHi Dee, in this instance there isn't any browser intelligence going on. As you say there's six HTML files - each HTML file has its own unordered list with the 'selected' class assigned, and each HTML file loads the same js file at the bottom of the page. So every time a page loads, the js starts again and looks through the current file for the list item with the 'selected' class.
If there was one unordered list written in a template file that was shared across all six pages, using the current code the js file wouldn't know what page it was on, and additional code would need to be written to retrieve the current url from the address bar as well as adding/removing classes to the list items.
Hopefully I've made it clear enough, if anything needs further clarification please let me know.
Dee K
17,815 PointsDee K
17,815 PointsHello Joel,
Thanks for your reply. From your reply, it seems as if you're saying, it is the "selected" class that determines how the web browser knows what page it is on?