-5

I'm wondering if Javascript can detect what page the user is on. Say for example that the user is on a "About" or "Contact" page, can JavaScript check if the pathway is "/About" or "/Contact" and then take action accordingly ? How would that code look like? I appreciate if anyone could give me a code sample

7
  • 1
    Yes, you can use document.location.href. Commented Nov 19, 2015 at 13:50
  • 2
    if(location.href=='/contacts')document.body.innerHTML=""; check the location, set the body html to empty string. Commented Nov 19, 2015 at 13:51
  • 2
    When you searched on Google for "javascript get current URL", you didn't find anything? Commented Nov 19, 2015 at 13:54
  • 2
    Look, I googled your title and came up with 9,640,000 results, and everything on the first page is exactly what people here are telling you to do... Commented Nov 19, 2015 at 14:00
  • 2
    So you people who are telling OP to search, do you not know how to close questions as a duplicate? You people sound very rude to a new user here. Commented Nov 19, 2015 at 14:02

2 Answers 2

1

For example, you can get the URL by doing this:

window.location.href
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to know what age they are on, it really depends on the url structure. But you will get the information with window.location.pathname

var pathname = window.location.pathname;
switch(pathname) {
   case "/home" :
       console.log("home");
       break;
   case "/game" :
       console.log("game");
       break;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.