views:

459

answers:

4

Hello,

I am currently building an making my new website. It uses one page and uses a jQuery accordian menu to load the content. The content it split up in six different div's and all load as soon as the page is accessed. I want it to load each div when the link to it is clicked on. To decrease the page loading time.

You can see this in action on my work in progress site here: http://is.gd/1p1Ys

Since I didn't make the jQuery script and I am really bad at JavaScript/jQuery I don't have a clue what to do. So I am wondering if anybody here can help me out.

Thanks a ton,

Ryan

A: 

From your description, it sounds like these divs are not displayed until the top-level menu item is clicked. If this is the case, you can use jquery to insert the sub-menu divs into the DOM when it is opened.

You can find a decent tutorial on Javascript/DOM Manipulation at w3schools.

Dana the Sane
A: 

I'm not sure the accordion control is what you are after - check out JQuery tabs - you can load the content via Ajax which will prevent them loading all at once. An explanation of how to do this is here

Macros
A: 

I would suggest looking at jquery-ui tabs if you want to load the content in dynamically. Currently I have not seen an accordian plugin that will handle it, which makes sense as an accordian needs to know the height of the content, also if when you clicked a panel you had to wait for dynamic content it would appear jerky. Tabs is your best and most appropriate way to move forward.

redsquare
+1  A: 

I agree with the other answers that jquery-ui tabs does this exact thing, but you can hack your accordian.js file pretty easily... just change the hrefs in your navigation to point to external html files containing the content you want, i.e. href="#lifestream" => href="lifestream.html",

and then in accordian.js:

$links.click(function () {
  var link = this,
  if(!link.href.match(/^#.*/)) { //if the href doesn't start with '#'
    loadDiv(link.href);
  } else {
    doRestOfFunction(); //everything your script is currently doing.
  }
});

function loadDiv(href) {
  $.get(href, function(html) {
   var newDiv = $(html)
   $(".panels").append(newDiv);
   newDiv.hide();
   doRestOfFunction(); //same as above
  }
}
Daniel
Thanks, do you know where to add this into accordian.js? Sorry if it is too much to ask but I have been fiddling around for the past 10 minutes and all i managed to do is make all the div's show on the main page. Thanks.
Ryan
1. remove all divs from your page.2. put them in their own .html files3. change the hrefs in the nav to point to lifestream.html etc4. create two functions -> loadDiv() and showDiv()5. loadDiv() => as above6. showDiv() => everything your $links.click() function is currently doing.7. change doRestOfFunction() => showDiv()
Daniel
Thanks for all the help but still isn't working =S
Ryan
sorry, got more complicated than i thought... i don't really understand everything ur doing. i don't think i can give an easy answer.
Daniel
Okay thanks for all the help anyway :)
Ryan