I'm very beginner in javascript and right now im learning from railscast. I have problem how to write this script, writen in coffee script to normal JS. Can somebody do this:
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
$('.pagination').text("Fetching more products...")
$.getScript(url)
$(window).scroll()
$('.tooltip').tooltipster(
animation: 'grow'
);
I try with " jQuery(function($) { " but it doesnt work.
3 Answers 3
I use http://js2coffee.org/ to convert between the two
1 Comment
You'd have to do it this way :
jQuery(function() {
if ($('.pagination').length) {
$(window).scroll(function() {
var url;
url = $('.pagination .next_page').attr('href');
if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
$('.pagination').text("Fetching more products...");
return $.getScript(url);
}
});
}
return $(window).scroll();
});
$('.tooltip').tooltipster({
animation: 'grow'
});
Wherever you see ->, change it to function() :)
10 Comments
$(document).ready(function(){
if ($('.pagination').length > 0)
$(window).scroll(function(){
var url = $('.pagination .next_page').attr('href')
if (url != null && $(window).scrollTop() > $(document).height() - $(window).height() - 50){
$('.pagination').text("Fetching more products...");
$.getScript(url);
}
});
$(window).scroll();
$('.tooltip').tooltipster(
animation: 'grow'
);
});
Changes:
--> => function(){}if url => if(url != null...)- some semicolons
; if length => if(length > 0)
As a side note: Include your script after including jQuery.
I saw you in a comment including a file in the js. That's not the way to include jQuery scripts or even pure js scripts in HTML. You need to do it like this(add this in the <head> of you HTML markup:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="your_file_name.js" type="text/javscript"></script>
5 Comments
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javscript"></script> in your HTML markup.
coffee -c file.coffee