?
Solved

Javascript clashing - trying to run two scripts on one page

Posted on 2009-03-30
4
Medium Priority
?
689 Views
Last Modified: 2012-05-06
Hello all,

I'm trying to incorporate two Jquery scripts in one XHTML page but they are clashing. I don't know anything about Jquery, having copyied the scripts from different sources. They work individually, just not together.

Is it possible to structure the attached code so that the scripts don't clash?

Many thanks,
Stuart
<script type="text/javascript" src="javascript/jquery.js"></script>
<script type="text/javascript" src="javascript/s3Slider.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#slider').s3Slider({
            timeOut: 3000
        });
        $('#slider1').s3Slider({
            timeOut: 4000 
        });
        $('#slider2').s3Slider({
            timeOut: 5000 
        });
        
    });
</script>
<script src="javascript/jquery-1.2.2.pack.js" type="text/javascript"></script>
 
<script type="text/javascript">
	$(function() {
		// set opacity to nill on page load
		$("ul#menu span").css("opacity","0");
		// on mouse over
		$("ul#menu span").hover(function () {
			// animate opacity to full
			$(this).stop().animate({
				opacity: 1
			}, "slow");
		},
		// on mouse out
		function () {
			// animate opacity to nill
			$(this).stop().animate({
				opacity: 0
			}, "slow");
		});
	});
</script>

Open in new window

0
Comment
Question by:stuamos8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 24023878
bYou're using one library (jquery) probably two different versions.
Just use one and get the latest version (1.3.2).
Get it at http://jquery.com

The two functions will be triggered via jquery when the DOM is loaded.
You can combine them as well.

See code
<script type="text/javascript" src="javascript/jquery.js"></script>
<script type="text/javascript" src="javascript/s3Slider.js"></script>
<script type="text/javascript">
    $(function() {
        $('#slider').s3Slider({
            timeOut: 3000
        });
        $('#slider1').s3Slider({
            timeOut: 4000 
        });
        $('#slider2').s3Slider({
            timeOut: 5000 
        });
        
        // set opacity to nill on page load
        $("ul#menu span").css("opacity","0");
        // on mouse over
        $("ul#menu span").hover(function () {
            // animate opacity to full
            $(this).stop().animate({
                opacity: 1
            }, "slow");
        },
        // on mouse out
        function () {
            // animate opacity to nill
            $(this).stop().animate({
                opacity: 0
            }, "slow");
        });
    });
</script>

Open in new window

0
 

Author Comment

by:stuamos8
ID: 24024159
Hi Albert,

Thanks for your response. I downloaded the latest jquery file and used your code but the scripts stil seem to clash (i.e the navigation menu animation doesn't work).

You can see the work in progress yourself at: http://bogrov.com/learnsync/clients.html. See the buttons working on the other pages.

Any ideas?

p.s. I found this on the subject: http://www.macpherson-neil.co.uk/ResourceWeb/WebDevelopment/Javascript/Multiscript_Tutorial2.htm - which seems to advise calling the events separately. I just don't know how to implement it.
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 2000 total points
ID: 24026299
Hey Stu

The problem is that you want to have 3 different sliders on 3 different elements. (on the divs with id slider, slider1 and slider2)
There is only the div with id slider.
Slider1 and slider2 do not exist in the html.

Just remove the statements.
<script type="text/javascript" src="javascript/jquery.js"></script>
<script type="text/javascript" src="javascript/s3Slider.js"></script>
<script type="text/javascript">
    $(function() {
        $('#slider').s3Slider({
            timeOut: 3000
        });
        
        // set opacity to nill on page load
        $("ul#menu span").css("opacity","0");
        // on mouse over
        $("ul#menu span").hover(function () {
            // animate opacity to full
            $(this).stop().animate({
                opacity: 1
            }, "slow");
        },
        // on mouse out
        function () {
            // animate opacity to nill
            $(this).stop().animate({
                opacity: 0
            }, "slow");
        });
    });
</script>

Open in new window

0
 

Author Closing Comment

by:stuamos8
ID: 31564551
Thanks Albert, you are my hero! Works perfectly.

I think it's about time I learn some Jquery. But in the meantime, thanks a bunch.

Cheers,
Stu
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 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