Welcome!

And don't forget to edit your signature & profile.

 

Icon

Statistics

  • Total posts 26942
  • Total topics 4894
  • Total members 6628
  • Our newest member
    Andylaidzzz

TOP POSTERS

Unique Functions (jQuery)

All problems related to JavaScript and AJAX are discussed and resolved here.
   

Unique Functions (jQuery)

Postby 3zzy » Tue Jun 02, 2009 11:56 am

Hi,

I'm using several jQuery plugins for my website and each page requires different functions, for example:

page1 has:

Code: Select all
   // navbar initialization
   $('#navbar').nav({ pathClass: 'current'   });
   $("#navbar li").find("a span").parent().addClass("l1");
   
   // dropdown
   $("ul.dropdown li").hover(function(){
      $(this).addClass("hover");
      $('ul:first',this).css('visibility', 'visible');
   }, function(){
      $(this).removeClass("hover");
      $('ul:first',this).css('visibility', 'hidden');
   });
   $("ul.dropdown li").find("a span").parent().addClass("l1");


page2 has:
Code: Select all
// tabs
   $('.tabs').each(function() {
    $(this).siblings('div').children('div:gt(0)').hide();
    $(this).children('a:first').addClass('active');
    $(this).children('a').click(function() {
      var current_content_div = '#' + $(this).attr('rel');
      $(this).siblings().removeClass('active');
      $(this).addClass('active');
      $(current_content_div).siblings().hide();
      $(current_content_div).show();
      return false;       
    });
  });


..can I do something to keep those initialization functions out from the html, at least until page load?

Thanks
— xHTML/CSS Ninja
User avatar
3zzy
Smashing <span>
 
Posts: 99
Joined: Sat Feb 14, 2009 11:33 pm
   

   

Re: Unique Functions (jQuery)

Postby Andy » Tue Jun 02, 2009 12:04 pm

Straight from the manual:

Code: Select all
$(document).ready(function(){
   // Your code here
});
Andy
Smashing <h5>
 
Posts: 1301
Joined: Tue Sep 30, 2008 6:42 pm
Location: Sweden
   

   

Re: Unique Functions (jQuery)

Postby 3zzy » Tue Jun 02, 2009 12:27 pm

Ofcourse I have those functions inside $(document).ready(function(). Let me explain it more clearly:

Page1 has:
Code: Select all
<script type="text/javascript">
//<![CDATA[
$(function() {
   // navbar
   $('#navbar').nav({ pathClass: 'current'   });
   $("#navbar li").find("a span").parent().addClass("l1");
   
   // dropdown
   $("ul.dropdown li").hover(function(){
      $(this).addClass("hover");
      $('ul:first',this).css('visibility', 'visible');
   }, function(){
      $(this).removeClass("hover");
      $('ul:first',this).css('visibility', 'hidden');
   });
   $("ul.dropdown li").find("a span").parent().addClass("l1");
});
//]]>
</script>


Page2 has:
Code: Select all
<script type="text/javascript">
//<![CDATA[
$(function() {
   // tabs
   $('.tabs').each(function() {
    $(this).siblings('div').children('div:gt(0)').hide();
    $(this).children('a:first').addClass('active');
    $(this).children('a').click(function() {
      var current_content_div = '#' + $(this).attr('rel');
      $(this).siblings().removeClass('active');
      $(this).addClass('active');
      $(current_content_div).siblings().hide();
      $(current_content_div).show();
      return false;       
    });
  });

});
//]]>
</script>


How can I merge all those callback functions into one single init.js?
— xHTML/CSS Ninja
User avatar
3zzy
Smashing <span>
 
Posts: 99
Joined: Sat Feb 14, 2009 11:33 pm
   

   

Re: Unique Functions (jQuery)

Postby Tarvalon » Tue Jun 02, 2009 1:01 pm

That didn't explain things any more clearly at all. You want your functions within the document.ready function as Andy mentioned. What aren't you understanding here?
I'm all about clean, semantic [X]HTML and CSS - when used properly the results are amazing! Always looking for freelance design and development work as well - PM me if you have a project that needs doing!
User avatar
Tarvalon
Smashing <li>
 
Posts: 438
Joined: Mon Feb 09, 2009 3:15 pm
Location: Chicago
   

   

Re: Unique Functions (jQuery)

Postby 3zzy » Tue Jun 02, 2009 1:16 pm

I want to store all those functions (pasted above) in a single, external init.js so I can keep my html clean, hope I'm making sense now?
— xHTML/CSS Ninja
User avatar
3zzy
Smashing <span>
 
Posts: 99
Joined: Sat Feb 14, 2009 11:33 pm
   

   

Re: Unique Functions (jQuery)

Postby Andy » Tue Jun 02, 2009 1:22 pm

3zzy wrote:I want to store all those functions (pasted above) in a single, external init.js so I can keep my html clean, hope I'm making sense now?


So cut the code out, paste it in init.js and load it with
Code: Select all
<script type="text/javascript" src="init.js"></script>


What's the problem, exactly?
Andy
Smashing <h5>
 
Posts: 1301
Joined: Tue Sep 30, 2008 6:42 pm
Location: Sweden
   

   

Re: Unique Functions (jQuery)

Postby 3zzy » Tue Jun 02, 2009 2:12 pm

Right, but then ALL (there will be 'many') functions all be loaded for each page, some also might conflict.
— xHTML/CSS Ninja
User avatar
3zzy
Smashing <span>
 
Posts: 99
Joined: Sat Feb 14, 2009 11:33 pm
   

   

Re: Unique Functions (jQuery)

Postby Andy » Tue Jun 02, 2009 2:23 pm

3zzy wrote:Right, but then ALL (there will be 'many') functions all be loaded for each page, some also might conflict.


... You are scripting with CSS selector engine. Utilize it.
Identify the markup and load things accordingly.
Andy
Smashing <h5>
 
Posts: 1301
Joined: Tue Sep 30, 2008 6:42 pm
Location: Sweden
   

   

Re: Unique Functions (jQuery)

Postby 3zzy » Tue Jun 02, 2009 3:10 pm

Thats what I don't understand and would like help with. How can I identify the page and appropriate load functions? Any examples?

Thanks
— xHTML/CSS Ninja
User avatar
3zzy
Smashing <span>
 
Posts: 99
Joined: Sat Feb 14, 2009 11:33 pm
   

   

Re: Unique Functions (jQuery)

Postby Andy » Tue Jun 02, 2009 3:24 pm

3zzy wrote:Thats what I don't understand and would like help with. How can I identify the page and appropriate load functions?


There are several ways of doing it.
You could identify individual elements in the source code using a CSS selector string. Remember, if it doesn't exist, the selection won't return anything. You could also identify pages as a whole by assigning IDs to the body tag and so forth.
Andy
Smashing <h5>
 
Posts: 1301
Joined: Tue Sep 30, 2008 6:42 pm
Location: Sweden
   


Return to JavaScript & AJAX



Who is online

Users browsing this forum: No registered users and 1 guest