I am working with jquery and multiple scripts that require javascript in the HTML document to function correctly (thankyou web 2.0). I am using ajax to post, $(document).ready
to run functions and other multiple events etc. I am using googles minify to help the load time of the external javascript files required to run the javascript in the HTML. I am also having trouble structuring/formatting my javascript.
My questions are as follows:
- How do I minimise the code in the HTML document?
- Is it possible to link the javascript in the HTML document externally
even if it requires
$(document).ready
, like my example below? - What is a good site or tutorial to assist me in correctly formatting my jquery/javascript as I am well aware this is incorrect and does this help load time?
Below is an example of a page where I run multiple scripts (feel free to format this correctly) and an example of what I would like to link externally and structure correctly. I am not asking for anyone to do my work for me but to simply just lead me in the right direction.
<script type="text/javascript" src="lib/js/jquery.nivo.slider.js"></script>
<script type="text/javascript" src="lib/js/jquery.fancybox.js"></script>
<script type="text/javascript" src="lib/js/jquery.jcarousel.min.js"></script>
<script type="text/javascript">
//nivo
$(window).load(function() {
$('#slider').nivoSlider({ effect: 'slideInLeft', pauseTime: 5000 });
});
//fancybox
$(document).ready(function() {
$('.fancybox').fancybox();
$.fancybox.open($("#welcome"), { padding : 0 });
});
//subscribe
$("#footer-subscribe-show-hide").click(function () {
$("#footer-subscribe").animate({width:'toggle'},300);
$(this).show("#subscribe");
});
//responsive
$(function() {
$('.menu-mobile-drop').click(function() {
$('.menu-mobile').toggle();
});
});
$(".menu-wrap").click(function() {
$(this).find('img').toggle();
});
//subscriptionAjax
$("#subscriber").submit(function(event) {
event.preventDefault();
$("#footer-subscribe").fadeOut();
var values = $(this).serialize();
$.ajax({
url: "include/subscribe.php",
type: "post",
data: values,
success: function(){
$("#footer-subscribe")
.html(
"<div class='subscription-success'>You're now subscribed!</div>"
)
.fadeIn('slow');
},
error: function(){
alert("failure");
$("#footer-subscribe").html('there is error while submit');
}
});
});
//jcarousel
function mycarousel_initCallback(carousel) {
carousel.clip.hover(function() {
carousel.stopAuto();
},
function() {
carousel.startAuto();
});
};
jQuery(document).ready(function() {
jQuery('#mycarousel').jcarousel({
auto: 8,
wrap: 'last',
initCallback: mycarousel_initCallback
});
});
</script>