views:

203

answers:

3

I have an html page (django) that contains several divs(each displaying different data) and one div that has navigation links. I'll call this my main page. I have an external .js file (jQuery) that reveals one display div on the main page and simultaneously hides all of the others (except the navigation div) based on which nav link is chosen.

$(function(){

$("#sl_sectorbr").click(function showSectorMini(){
  $(".minimenu").hide();
  $(".minimenu > *").hide();
  $("#sector_mini").fadeIn("slow");
  $("#sector_mini > *").fadeIn("slow");
});

All of this works fine. My question is, if I want to place "navigation links" on a second html page; that when clicked would both load my main page AND call/execute a specific function like "showSectorMini()" just as if it were clicked from the main page itself — how would I write that code? I'd really appreciate any help I could get.

A: 

You should arrange to produce different versions of the page, and put different onload actions into each version. For example, make the div to show a query parameter, and make django fill in the right onload depending on the query parameter. Then put the different query parameters into the links.

Martin v. Löwis
I started to write back that this scenario wouldn't solve my problem, and in parsing out my argument it occurred to me that, uhmm it actually might. Though the crux of the challenge is simple enough, there's actually a fair amount of data that I'm trying to organize so I have to wrap my mind around how all of this is going to work. I'm going to work with your solution and see where it takes me. thanks so much for your help.
+1  A: 

You could use the hash - link to http://example.com/#sectionOne and read the hash in your ready function.

Sidnicious
thanks Sidney. I'm a little unsure as to the exact syntax/procedure for this method, but will definitely study it as it sounds promising. thanks again.
+1  A: 

As SidneySM suggested, a hash is the standard way of handling this. It could go something like this:

In your external js file:

var initSectorUI = function(){
  if (location.hash) showSectorMini(location.hash);
};

var showSectorMini = function(sector){
  $('#sectors > div').hide();
  $(sector).show();
};

On your other pages:

$(function(){
  initSectorUI();

  $("#navigator a").click(function(){ 
    showSectorMini($(this).attr('href'));
  });
});

<div id="navigator">
  <a href="#one">One</a>
  <a href="#two">Two</a>
</div>
<div id="sectors">
  <div id="one" style="display:none">A one</div>
  <div id="two" style="display:none">A two</div>
</div>
tom
awesome! i think this is exactly what i need. i'll add the code and let you know how it works. really appreciate your help Tom. (and everyone else as well!)
This is a pretty standard pattern that's used most commonly in UI tabs: http://flowplayer.org/tools/tabs.html You might consider using an existing library as it will give you additional functionality like browser history (back button support).
tom
learning how to write 'best practice' code is important to me, which is why your comment is so valuable. i'll try and code it myself first so I can gain practice doing it, then i'll investigate the library option because ultimately i'll certainly want the back button support and additional functionality. thanks again!