Question about JS "home page with no html file"

In chapter 76 in the A Smarter Way to Learn JS this is the question of an exercise:

Code the first line of an if statement that tests whether the home page with no html file specified is currently loaded in the browser.

The answer is:

if (window.location.pathname === "/") {

I could not understand the use of"/"

Any indepth insight or link will be welcome.

The “/” is the root “url” essential. It usually points to the homepage of every website. “window” is the global window object. “location” and “pathname” are properties on that object. It is saying if the pathname of the current location (this info is stored in the window object) is “root” or “/” you can logically assume you are on the homepage of a web app.

1 Like

Any idea why he said “home page with no html file specified

And why did he use pathname not hostname?

You aren’t looking for the hostname you’re looking for the pathname so that would be why they did it that way. Usually the home page is an index.html file so my guess is that this “path” doesn’t have an html file specified. I’ve never read this book so I can’t speak for what the author is talking about in the greater context just what that line you indicated meant.

When I call a web page, I can call it as:

That specifies which file. If it’s in a public directory the server says “OK, they want that file and it’s in a public directory so I’ll send it back.”

Another way to access that web page is:

In that case, the server sees it come in to an empty pathname. If the server has instructions of what to do on an empty pathname. Very commonly the default is to look for index.html and send it back.

The the first case, the pathname will be “/index.html” (I think) and in the second it will be “/” (an empty path). They very often will go to the same file, index.html.

In theory, you could have the empty path route to something else, the server could send it to “” or whatever.

This will all make more sense when you get to the backend section and build servers and routes and serve files yourself. Until then, it all seems like magic.

1 Like