If user came from previous page on site then this, else do this

You can get the page the user came from with document.referrer.

So you could implement your solution like this:

if (document.referrer === 'yoursite.com/parentpage') {
  // do bar
} else {
  // do foo
}

Please try this

This code in second page

jQuery(window).load(function() {
  if (sessionStorage.getItem('dontLoad') == null) {
    //show bar
  }
  else{
    //show foo
  }
});

This code in parent page

jQuery(window).load(function() {
  sessionStorage.setItem('dontLoad','true') 
});

with php:

There is a simple way is to create a mediator page which redirect to inner page after make a session / cookie.. then if you'll get session / cookie, you show foo & unset session.

if someone directly come from url, no session / cookie found & it show bar..