1

I have two pages here: index and results. On the results.html, I have some divs with CSS property set as display: none. I would like to navigate from index (nav menu) and open the results pages while displaying the div that was hidden. The jQuery works fine on the results.html, but I don't know how can I trigger this function coming from the index.

Index:

<ul>
   <li><a id="menu-2016" href="results.html#2016">2016</a></li>
   <li><a id="menu-2015" href="results.html#2015">2015</a></li>
</ul>

Results:

<section id="2016">Content</section>
<section id="2015">Content</section>

CSS:

#section-2015, #section-2016 {
    display: none;
}

jQuery:

$(document).ready(function () {
    $("#menu-2016").click(function () {
        $("#section-2016").slideDown();
        $("#section-2015").slideUp();
    });
    $("#menu-2015").click(function () {
        $("#section-2015").slideDown();
        $("#section-2014").slideUp();
    });
});
rrk
  • 15,677
  • 4
  • 29
  • 45
h1ghland3r
  • 106
  • 1
  • 1
  • 9

3 Answers3

1

You can simply check the URL hash/fragment.

$(document).ready(function() {
    if (window.location.hash == '#2016') {
       $("#section-2016").slideDown();
       $("#section-2015").hide();
    }
    if (window.location.hash == '#2015') {
       $("#section-2016").hide();
       $("#section-2015").slideDown();
    }
});
Andrew Khan
  • 198
  • 9
1

You could use the

window.name

property. The window object that is always instantiated via Javascript has that property and that stays always the same when loading new documents in the same window. So set the window.name to the page you where coming from. Do that on page load of your index file. In your result file you test that condition on page load and when it's your index file just display your hidden content corresponding to it.

Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22
0

I would think, send your params as actual params:

 <ul>
     <li><a id="menu-2016" href="results.html?year=2016">2016</a></li>
     <li><a id="menu-2015" href="results.html?year=2015">2015</a></li>
 </ul>

And parse the params as described here: How can I get query params...

Community
  • 1
  • 1