0

I'm not even sure what I'm thinking of will work and can't seem to find the right wording to get and search results that are remotely helpful so here goes.

What I want to be able to do is have a link from one page then cause the linked page to display a certain way. The code below is the script being used on the page I'll be linking to.

 $(document).ready(function() {
            $('.hideshow').click(function () {
                var name = $(this).attr('id').replace("-L","");
                if ($(this).hasClass("hidden")) {
                    $(this).addClass("shown");
                    $(this).removeClass("hidden");
                    $('div#' + name).show(500);
                } else {
                    $(this).addClass("hidden");
                    $(this).removeClass("shown");
                    $('div#' + name).hide(500);
                }
            });
        });

This code will hide or show content when links on the page are clicked using the id names used in the body of the file. Now what I want to be able to do is have the link from the previous page indicate certain links on this page as being shown. The following is some of the in body code.

<a class="hideshow hidden" id="cat-articles-L" style="cursor:pointer;"><font style="font-size:24px; color:#06F; text-decoration:underline;"> Cat Articles</font></a><br />
                   <div id="cat-articles" style="display:none;">
                   &nbsp;&nbsp;&nbsp;&nbsp;<a class="hideshow hidden" id="cat-beds-L" style="cursor:pointer;"><font style="font-size:18px; color:#06F; text-decoration:underline;">Cat Beds</font></a><br />

On default the "Cat Articles" are visible but "Cat Beds" is hidden until "Cat Articles" is clicked, then there could be sublevels so more items under "Cat Beds" The idea is when you link from the other page having it load with certain items already open. Hope I made this clear enough, still new to this site and posting questions.

JClaspill
  • 1,725
  • 19
  • 29
Darren M
  • 11
  • 1

1 Answers1

0

Add a parameter to the link on the original page. Something like domain.com/page.html?id=5 and then have javascript check for a QueryString of id and do something like a switch with it.

See How can I get query string values in JavaScript? for how to get QueryStrings if you aren't familiar.

If you need to pass multiple of the same type (example, multiple IDs), you can make an array and pass that array to the other page. In that array, include every id you want to display.

See an example of an array to url here: JavaScript Array to URLencoded

Example (Original Page):

var idArray = new Array(1,5,15,38);
var url = "http://www.blah.com/link.html?"+idArray.join('&');

Example (Linked page):

var qs = (function(a) {
    if (a == "") return {};
    var b = {};
    for (var i = 0; i < a.length; ++i)
    {
        var p=a[i].split('=');
        if (p.length != 2) continue;
        b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
})(window.location.search.substr(1).split('&'));
var idArray = qs["id"];
for (var i = 0; i < idArray.length; i++) {
    switch(idArray[i]){
        case 1:
            //show link1
            break;
        case 2:
            //show link3
            break;
        default:
            //do nothing, but required
            break;
    }
}

Note: It may look that I'm using JQuery, but I'm not. This will work without it just fine.

Community
  • 1
  • 1
JClaspill
  • 1,725
  • 19
  • 29
  • Not entirely sure I follow, but can I pass multiple ids this way, some of what I want to be able to do is multiply levels deep so id=5 may be top level and it needs to display but id=25 is inside id=5 and those two are the only ones I want showing up. Is that possible through your solution? – Darren M Jul 24 '13 at 21:54
  • @DarrenM Added some code examples. Hopefully those give a better idea of what I am referring to. If you are doing this with PHP you can use Sessions to pass this between pages. I just didn't mention it before since you've never specified if you are using php/asp.net/etc – JClaspill Jul 24 '13 at 22:20
  • I'm assuming the numbers you used in the array are just examples and I can swap them out for the named ids used on my linked page. Also is there something I need to be doing to the link itself where it is linking on the page? Also the linked page is a .aspx file – Darren M Jul 25 '13 at 14:43