Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Jquery with rails 3

Posted on 2010-11-10
3
Medium Priority
?
954 Views
Last Modified: 2012-05-10
this jquery code works when referenced internally but when i place it in the application.js file. it dosen't work
working when referenced like this:

##application.html.erb
<script type="text/javascript">  
	$(function () {
		setInterval("rotateImages()", 2000);

	});

	function rotateImages () {
		var oCurPhoto = $("#slideshow div.current");
		var oNxtPhoto = oCurPhoto.next();
		if (oNxtPhoto.length == 0)
			oNxtPhoto = $("#slideshow div:first");

		oCurPhoto.removeClass('current').addClass('previous');
		oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity:1.0 }, 1000, 
			function () {
				oCurPhoto.removeClass('previous');
			});	
	}


but not like this:

##application.html.erb
	<%= javascript_include_tag "application" %>
  <%= javascript_include_tag "jquery-1.4.3" %>
	<%= javascript_include_tag "rails" %>
  <%= csrf_meta_tag %>

##application.js
(function () {
	setInterval("rotateImages()", 2000);

});

function rotateImages () {
	var oCurPhoto = $("#slideshow div.current");
	var oNxtPhoto = oCurPhoto.next();
	if (oNxtPhoto.length == 0)
		oNxtPhoto = $("#slideshow div:first");

	oCurPhoto.removeClass('current').addClass('previous');
	oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity:1.0 }, 1000, 
		function () {
			oCurPhoto.removeClass('previous');
		});	
}

Open in new window

0
Comment
Question by:depassion
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
kristinalim earned 2000 total points
ID: 34104211
Try moving your include tag for application.js after the include tag for the jQuery library. The problem with your situation seems to be that when your application.js is loaded, the dollar sign ($) is not yet defined, something that the jQuery library takes care of.
0
 
LVL 12

Expert Comment

by:JESii
ID: 34104375
I believe that's partially correct; but the standard Rails JS library (Prototype) also uses the $ tag, so I think you have to also tell jQuery to redefine the $ tag to something else, like this:
  jQuery.noConflict();
That will change the jQuery $ variable to jQuery

See http://api.jquery.com/jQuery.noConflict/ for more information.
0
 
LVL 4

Expert Comment

by:kristinalim
ID: 34104510
Definitely as JESii said, you would need jQuery.noConflict() if you also include the Prototype library.

By default the Prototype library is included in the Javascript expansion :defaults, so you need this if have something like:

<%= javascript_include_tag :defaults %>

or:

<%= javascript_include_tag "prototype" %>

in your layout.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question