/    Sign up×
Community /Pin to ProfileBookmark

Why is my Javascript interfering with each other?

I’m very new to this, but I have two javascripts that I’m trying to implement on my site, but one is interfering with the other…I’ve checked that the variables are unique (I think) and I’m lost as to why. 😑

here are the two scripts that I’m using:

the first (a slide panel):

[CODE]

window.addEvent(‘domready’, function(){

$(‘login’).setStyle(‘height’,’auto’);
var mySlide = new Fx.Slide(‘login’).hide(); //starts the panel in closed state

$(‘toggleLogin’).addEvent(‘click’, function(e){
e = new Event(e);
mySlide.toggle();
e.stop();
});

$(‘closeLogin’).addEvent(‘click’, function(e){
e = new Event(e);
mySlide.slideOut();
e.stop();
});

});[/CODE]

And the second script (a menu):

[CODE]

$(document).ready(function(){

$(“#nicemenu img.arrow”).click(function(){

$(“span.head_menu”).removeClass(‘active’);

submenu = $(this).parent().parent().find(“div.sub_menu”);

if(submenu.css(‘display’)==”block”){
$(this).parent().removeClass(“active”);
submenu.hide();
$(this).attr(‘src’,’images/arrow_hover.png’);
}else{
$(this).parent().addClass(“active”);
submenu.fadeIn();
$(this).attr(‘src’,’images/arrow_select.png’);
}

$(“div.sub_menu:visible”).not(submenu).hide();
$(“#nicemenu img.arrow”).not(this).attr(‘src’,’images/arrow.png’);

})
.mouseover(function(){ $(this).attr(‘src’,’images/arrow_hover.png’); })
.mouseout(function(){
if($(this).parent().parent().find(“div.sub_menu”).css(‘display’)!=”block”){
$(this).attr(‘src’,’images/arrow.png’);
}else{
$(this).attr(‘src’,’images/arrow_select.png’);
}
});

$(“#nicemenu span.head_menu”).mouseover(function(){ $(this).addClass(‘over’)})
.mouseout(function(){ $(this).removeClass(‘over’) });

$(“#nicemenu div.sub_menu”).mouseover(function(){ $(this).fadeIn(); })
.blur(function(){
$(this).hide();
$(“span.head_menu”).removeClass(‘active’);
});

$(document).click(function(event){
var target = $(event.target);
if (target.parents(“#nicemenu”).length == 0) {
$(“#nicemenu span.head_menu”).removeClass(‘active’);
$(“#nicemenu div.sub_menu”).hide();
$(“#nicemenu img.arrow”).attr(‘src’,’images/arrow.png’);
}
});

});

[/CODE]

There are also are two other scripts that are involved with the first one, but I’m not sure if they’re the problem.

mootools-1.2-core.js
mootools-1.2-more.js

Should I paste that code as well? Or is the problem in the first two?

Thanks very much for any advice or direction!

to post a comment
JavaScript

13 Comments(s) ↴

Copy linkTweet thisAlerts:
@Jeff_MottAug 23.2008 β€”Β It looks like you're using both MooTools and jQuery. The problem is they both use "$".

Fortunately jQuery has a "noConflict" method that lets it play nice with any other library. At the earliest opportunity after jQuery is loaded, call [font=courier]jQuery.noConflict()[/font], and from there on out "$" won't represent jQuery anymore, and instead you'll call jQuery with the name "jQuery" itself. For example, [font=courier]jQuery(document).ready(function(){[/font]
Copy linkTweet thisAlerts:
@jasmine123authorAug 23.2008 β€”Β I'm not sure how to follow your advice. Where would I put the

jQuery.noConflict()

sorry to be dense
Copy linkTweet thisAlerts:
@jasmine123authorAug 23.2008 β€”Β ok, I learned where to put the jQuery.noConflict()

and I inserted this code:

[CODE]<script>

jQuery.noConflict();

// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
jQuery("div").hide();
});

// Use Mootools with $(...), etc.
$('someid').hide();

</script>

[/CODE]


I changed all the variables in the jQuery script like this:

[CODE]

jQuery(document).ready(function(){

jQuery("#nicemenu img.arrow").click(function(){

jQuery("span.head_menu").removeClass('active');

submenu = jQuery(this).parent().parent().find("div.sub_menu");

if(submenu.css('display')=="block"){
jQuery(this).parent().removeClass("active");
submenu.hide();
jQuery(this).attr('src','images/arrow_hover.png');
}else{
jQuery(this).parent().addClass("active");
submenu.fadeIn();
jQuery(this).attr('src','images/arrow_select.png');
}

jQuery("div.sub_menu:visible").not(submenu).hide();
jQuery("#nicemenu img.arrow").not(this).attr('src','images/arrow.png');

})
.mouseover(function(){ jQuery(this).attr('src','images/arrow_hover.png'); })
.mouseout(function(){
if(jQuery(this).parent().parent().find("div.sub_menu").css('display')!="block"){
jQuery(this).attr('src','images/arrow.png');
}else{
jQuery(this).attr('src','images/arrow_select.png');
}
});

jQuery("#nicemenu span.head_menu").mouseover(function(){ jQuery(this).addClass('over')})
.mouseout(function(){ jQuery(this).removeClass('over') });

$("#nicemenu div.sub_menu").mouseover(function(){ jQuery(this).fadeIn(); })
.blur(function(){
jQuery(this).hide();
jQuery("span.head_menu").removeClass('active');
});

jQuery(document).click(function(event){
var target = jQuery(event.target);
if (target.parents("#nicemenu").length == 0) {
jQuery("#nicemenu span.head_menu").removeClass('active');
jQuery("#nicemenu div.sub_menu").hide();
jQuery("#nicemenu img.arrow").attr('src','images/arrow.png');
}
});


});

[/CODE]


But it didn't seem to work. I'm not sure what to put here...

[CODE] $('someid').hide();
[/CODE]


thanks for any help
Copy linkTweet thisAlerts:
@Jeff_MottAug 23.2008 β€”Β At this point you'll need to provide a link to the actual page so we can see what error messages there might be.
Copy linkTweet thisAlerts:
@jasmine123authorAug 23.2008 β€”Β not sure how to provide a working link.
Copy linkTweet thisAlerts:
@avitardotnetAug 23.2008 β€”Β not sure how to provide a working link.[/QUOTE]

If it is on a website then you need to copy the address in the browser and paste it in the forum using the link icon (globe with a chain).

If it is just being worked on in your local PC and can not be reached on the web then you should instead copy the code for the html document you are using.

just as a personal note I strongly recommend not using either of these two tool boxes (jQuery and mootools) since both of them are badly written and tend to create a good deal of overhead for visitors on websites.
Copy linkTweet thisAlerts:
@Jeff_MottAug 23.2008 β€”Β ...since both of them are badly written...[/quote]How so?

...and tend to create a good deal of overhead for visitors on websites[/quote]I can only speak for jQuery. And when jQuery is compressed properly, it's less than 15K. I've seen JPEGs bigger than that. So it doesn't make nearly the "good deal of overhead" you claim it to.
Copy linkTweet thisAlerts:
@jasmine123authorAug 23.2008 β€”Β [CODE]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script>
jQuery.noConflict();
// Do something with jQuery
jQuery("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';
</script>


<script type="text/javascript" src="scripts/menu.js"></script>

<!-- START Fx.Slide -->
<!-- The CSS -->

<link rel="stylesheet" href="fx.slide.css" type="text/css" media="screen" />
<!-- Mootools - the core -->
<script type="text/javascript" src="scripts/mootools-1.2-core-yc.js"></script>
<!--Toggle effect (show/hide login form) -->
<script type="text/javascript" src="scripts/mootools-1.2-more.js"></script>
<script type="text/javascript" src="scripts/fx.slide.js"></script>
<!-- End Fx.Slide -->



</head>


/*navigation*/
#navigation {
float: left;
height: 30px;
width: 600px;
color: #333;
padding: 10px;
border: none;
margin: 0px 0px 0px 0px;
background: none;
}
ul, li{margin:0; border:0; list-style:none; padding:0;}
ul{height:21px;}

h1 { font-size:18px; }
p { line-height:18px; }

#info { margin:auto; width:600px; color:#333333; padding:10px; background:#f4f4f4; border:1px solid #DDD; }

#nicemenu { margin:0 0px 0 195px; width:615px; border-bottom:none; height:23px; }
#nicemenu a { color:#8a7e5a; text-decoration:none; }
#nicemenu a:hover { text-decoration:underline; }
#nicemenu li { display:inline; position:relative; padding-left: 5px; }
#nicemenu li span { position:relative; z-index:10; padding:4px 4px 4px 6px; border-bottom:none; line-height:18px; }
#nicemenu li span a { font-weight:normal; padding:0 8px 0px 2px; font-size:12px; font-family:"Lucida Grande"; }
#nicemenu li span.over { padding:4px 3px 4px 5px; border-top:solid 1px #E5E5E5; border-left:solid 1px #E5E5E5; border-right:solid 1px #999999; border-bottom:solid 1px #fff; }
*+html #nicemenu li span.over { border-top:solid 2px #E5E5E5; padding-bottom:3px; } /* IE6 */
#nicemenu li span.over a { }
#nicemenu li span.over a:hover { text-decoration:none; }
#nicemenu li span.active { padding:4px 3px 4px 5px; border-top:solid 1px #E5E5E5; border-left:solid 1px #E5E5E5; border-right:solid 1px #999999; border-bottom:solid 1px #fff; }
*+html #nicemenu li span.active { border-top:solid 2px #E5E5E5; padding-bottom:3px; }
#nicemenu li span.active a { }
#nicemenu li span.active a:hover { text-decoration:none; }
#nicemenu img.arrow { /*margin-left:4px;*/ cursor:pointer; }
#nicemenu div.sub_menu { display:none; position:absolute; left:0; top:0px; margin-top:18px; border-top:solid 1px #E5E5E5; border-left:solid 1px #E5E5E5; border-right:solid 1px #999999; border-bottom:solid 1px #999999; padding:4px; top:2px; width:160px; background:#FFFFFF; }
* html #nicemenu div.sub_menu { margin-top:23px; } /* IE6 */
*+html #nicemenu div.sub_menu { margin-top:23px; } /* IE7 */
#nicemenu div.sub_menu a:link,
#nicemenu div.sub_menu a:visited,
#nicemenu div.sub_menu a:hover{ display:block; font-size:11px; padding:4px;}
#nicemenu a.item_line { border-top:solid 1px #E5E5E5; padding-top:6px !important; margin-top:3px; }

/*end navigation*/

/*start top panel*/
#top-panel{
background:#e8f3c6;
border-bottom:3px solid #a6c34e;
padding:14px 20px;
text-align:right;
}
#sub-panel{
text-align:center;
}
#sub-panel a{
width:150px;
float:right;
color:#FFFFFF;
text-decoration:none;
margin-right:30px;
font-weight:bold;
background:url(images/sub-left.png) bottom left no-repeat #a6c34e;
}
#sub-panel a span{
padding:6px;
background:url(images/sub-right.png) right bottom no-repeat;
display:block;

}
strong{color:#000000;}
.face{border:solid 2px #a6c34e; margin-left:10px; float:right;}
/*end top panel*/


</style>

<body>

<div id="login">
<div class="loginContent">
<form action="#" method="post">
<label for="log"><b>Username: </b></label>
<input class="field" type="text" name="log" id="log" value="" size="23" />
<label for="pwd"><b>Password:</b></label>
<input class="field" type="password" name="pwd" id="pwd" size="23" />

<input type="submit" name="submit" value="" class="button_login" />
<input type="hidden" name="redirect_to" value=""/>
</form>
<div class="left">
<label for="rememberme"><input name="rememberme" id="rememberme" class="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label></div>
<div class="right">Not a member? <a href="#">Register</a> | <a href="#">Lost your password?</a></div>

</div>
<div class="loginClose"><a href="#" id="closeLogin">Close Panel</a></div>
</div> <!-- /login -->

<div id="container">
<div id="top">
<!-- login -->
<ul class="login">

<li class="left">&nbsp;</li>
<li>Hello!</li>
<li>|</li>
<li><a id="toggleLogin" href="#">Log In</a></li>
</ul> <!-- / login -->
</div> <!-- / top -->

<div class="clearfix"></div>
</div>


<!-- Begin Wrapper -->
<div id="wrapper">

<!-- Begin Header -->
<div id="header">

<img src='images/head.jpg' style="margin-left:240px">

</div>
<!-- End Header -->


<!-- Begin Navigation -->
<div id='navigation'>
<div id="nicemenu">
<ul>
<li><span class="head_menu"><a href="index.html">home</a></span>
<li><span class="head_menu"><a href="index.html">collections</a><img src="images/arrow.png" width="18" height="20" align="top" class="arrow" /></span>
<div class="sub_menu">
<a href="index.html">Your Photos</a>

<a href="index.html">Your Sets</a>
<a href="index.html">Your Archives</a>
<a href="index.html">Your Tags</a>
<a href="index.html">Your Map</a>
<a href="index.html">Your Favorites</a>
<a href="index.html">Your Stats</a>

<a href="index.html" class="item_line">Recent Activity</a>
<a href="index.html">Comments You've Made</a>
<a href="index.html" class="item_line">Upload Photos</a>
<a href="index.html" class="item_line">Your Account</a>
<a href="index.html">Your Profile</a>
<a href="index.html" class="item_line">FlickrMail</a>

</div>
</li>
<li><span class="head_menu"><a href="index.html">warranty</a><img src="images/arrow.png" width="18" height="20" align="top" class="arrow" /></span>
<div class="sub_menu">
<a href="index.html">All your photos</a>
<a href="index.html">Most recently uploaded photos</a>
<a href="index.html">Your Sets</a>

<a href="index.html">Your Map</a>
</div>
</li>
<li><span class="head_menu"><a href="index.html">retailers</a><img src="images/arrow.png" width="18" height="20" align="top" class="arrow" /></span>
<div class="sub_menu">
<a href="index.html">Latest Photos</a>
<a href="index.html">Contact List</a>

<a href="index.html">People Search</a>
<a href="index.html" class="item_line">Invite your Friends</a>
<a href="index.html">Invite History</a>
<a href="index.html">Guest Pass History</a>
<a href="index.html" class="item_line">Give the gift of Flickr</a>
</div>

</li>
<li><span class="head_menu"><a href="index.html">links</a><img src="images/arrow.png" width="18" height="20" align="top" class="arrow" /></span>
<div class="sub_menu">
<a href="index.html">Your Groups</a>
<a href="index.html">Recent Changes</a>
<a href="index.html" class="item_line">Search for a Group</a>
<a href="index.html" class="item_line">Create a New Group</a>

</div>
</li>
<li><span class="head_menu"><a href="index.html">contact</a><img src="images/arrow.png" width="18" height="20" align="top" class="arrow" /></span>
<div class="sub_menu">
<a href="index.html">Explore Page</a>
<a href="index.html">Last 7 Days Interesting</a>
<a href="index.html">Calendar</a>

<a href="index.html">A Year Ago Today</a>
<a href="index.html" class="item_line">World Map</a>
<a href="index.html">Places</a>
<a href="index.html">Camera Finder</a>
<a href="index.html" class="item_line">Popular Tags</a>
<a href="index.html">Most Recent Photos</a>

<a href="index.html">Creative Commons</a>
<a href="index.html" class="item_line">FlickrBlog</a>
<a href="index.html" class="item_line">Do More with Your Photos</a>
<a href="index.html">Flickr Services</a>
</div>
</li>
</ul>

</div>
</div>
<!-- End Navigation -->

<!-- Begin Left Column -->
<div id="leftcolumn">



<img src='images/thumb.jpg' style="margin-left:349px">







</div>
<!-- End Left Column -->


<!-- Begin Right Column -->
<div id="rightcolumn">



</div>
<!-- End Right Column -->

</div>
<!-- End Wrapper -->



</body>
<div id="footer">coinwatch north america / castle peak web design</div>

</html>
[/CODE]
Copy linkTweet thisAlerts:
@Jeff_MottAug 23.2008 β€”Β Jasmine, the problem is you're trying to use the libraries before you've loaded them. The script tags that include the libraries into the page must come first. I also don't see the jQuery library being included at all.
Copy linkTweet thisAlerts:
@jasmine123authorAug 23.2008 β€”Β Thank you, Jeff.

I arranged the order like this:


[CODE]

<head>
<script>
jQuery.noConflict();
// Do something with jQuery
jQuery("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';
</script>


<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">

jQuery(document).ready(function(){

jQuery("#nicemenu img.arrow").click(function(){

jQuery("span.head_menu").removeClass('active');

submenu = jQuery(this).parent().parent().find("div.sub_menu");

if(submenu.css('display')=="block"){
jQuery(this).parent().removeClass("active");
submenu.hide();
jQuery(this).attr('src','images/arrow_hover.png');
}else{
jQuery(this).parent().addClass("active");
submenu.fadeIn();
jQuery(this).attr('src','images/arrow_select.png');
}

jQuery("div.sub_menu:visible").not(submenu).hide();
jQuery("#nicemenu img.arrow").not(this).attr('src','images/arrow.png');

})
.mouseover(function(){ jQuery(this).attr('src','images/arrow_hover.png'); })
.mouseout(function(){
if(jQuery(this).parent().parent().find("div.sub_menu").css('display')!="block"){
jQuery(this).attr('src','images/arrow.png');
}else{
jQuery(this).attr('src','images/arrow_select.png');
}
});

jQuery("#nicemenu span.head_menu").mouseover(function(){ jQuery(this).addClass('over')})
.mouseout(function(){ jQuery(this).removeClass('over') });

jQuery("#nicemenu div.sub_menu").mouseover(function(){ jQuery(this).fadeIn(); })
.blur(function(){
jQuery(this).hide();
jQuery("span.head_menu").removeClass('active');
});

jQuery(document).click(function(event){
var target = jQuery(event.target);
if (target.parents("#nicemenu").length == 0) {
jQuery("#nicemenu span.head_menu").removeClass('active');
jQuery("#nicemenu div.sub_menu").hide();
jQuery("#nicemenu img.arrow").attr('src','images/arrow.png');
}
});


});

</script>


<!-- START Fx.Slide -->
<!-- The CSS -->
<link rel="stylesheet" href="fx.slide.css" type="text/css" media="screen" />
<!-- Mootools - the core -->
<script type="text/javascript" src="scripts/mootools-1.2-core-yc.js"></script>
<!--Toggle effect (show/hide login form) -->
<script type="text/javascript" src="scripts/mootools-1.2-more.js"></script>
<script type="text/javascript" src="scripts/fx.slide.js"></script>
<!-- End Fx.Slide -->


</head>[/CODE]


And it worked!

Thanks very much, again.
Copy linkTweet thisAlerts:
@avitardotnetAug 23.2008 β€”Β How so?

I can only speak for jQuery. And when jQuery is compressed properly, it's less than 15K. I've seen JPEGs bigger than that. So it doesn't make nearly the "good deal of overhead" you claim it to.[/QUOTE]


not to get too far off topic but some of the reasons I advise people not use these so called api's are because:

  • 1. They are not true API's.. serveral of their features are just duplications of existing functionality built into javascript as opposed to creating functionality that does not already exist in javascript, yet exists in other programming languages. Some good examples of true API-ish functions would be Trim, InArray, etc. not visiblity toggling
    [CODE]$("a").click(function() {
    $('#Content').toggle();

    }); [/CODE]

    which is a waste of code. At best it is just these and others like prototype are lame bloated tool kits.


  • 2. You state around 15 kb, but that is an few seconds of wasted download time compared to 2-3 kb for a true useful api, not to mention the client side processing overhead that results in a few seconds of waste processing time.


  • 3. They themselves are a lot of superfluous coding that is not environmentally friendly; ie they cause conflicts with other tool-kits as well as introduce possible bugs.


  • 4. They encourage a poor programming practices -- using proprietary function names that will break if you were to move your code to a different api.. forcing you to write a bridge, etc.


  • 5. A host of other issues which are too lengthy to write down here.


  • Lets just say I haven't really seen any good public api's for javascript yet over the last 12 years of javascript programming I have done.
    Copy linkTweet thisAlerts:
    @Jeff_MottAug 24.2008 β€”Β They are not true API's.. serveral of their features are just duplications of existing functionality built into javascript as opposed to creating functionality that does not already exist in javascript[/quote]CSS2 & 3 selectors? XPath? Commonly used animations, like fades and slides? Cross-browser compatible events?

    jQuery offers a tremendous amount of features that aren't built into JavaScript.

    $("a").click(function() {

    $('#Content').toggle();

    });[/quote]
    That particular example happens to map nicely to native functionality, yes, but there are plenty of situations that don't map nicely. A jQuery selector I wrote recently is [font=courier]$("#top .search label")[/font]. If I were to write that manually, I'd be getting an element by ID, searching through all its descendants for a particular class name, and then getting an element by tag name for each of those.

    The jQuery version is much more concise and doesn't bog you down with the details.

    ...not environmentally friendly; ie they cause conflicts with other tool-kits...[/quote]For MooTools and Prototype, you're probably right. jQuery is one of the exceptions. The only names it exports to the global namespace are "jQuery" and "$". And if you ask it to, it will not touch "$".

    They encourage a poor programming practices -- using proprietary function names...[/quote]You don't seem to understand the word "proprietary". You should [url=http://www.merriam-webster.com/dictionary/proprietary]look it up[/url] before you misuse it further.

    And no, using a library is not a poor programming practice. In fact, the opposite is true. Would you say that using libraries in PHP is bad practice? What about in Java? You're holding JavaScript and jQuery to a double standard. The fact is that using well known and well tested libraries is actually considered very good practice.
    Copy linkTweet thisAlerts:
    @Declan1991Aug 24.2008 β€”Β 4. They encourage a poor programming practices -- using proprietary function names that will break if you were to move your code to a different api.. forcing you to write a bridge, etc.[/quote]
    Assuming you've read Jeff's post, obviously if you remove the library, the code doesn't work. It's like saying object orientated programming is bad practice because if you remove the class definitions and switch to a different class you must write a bridge.

    Lets just say I haven't really seen any good public api's for javascript yet over the last 12 years of javascript programming I have done.[/quote]And you never will because the code is guaranteed to break if you remove the library, as the library's API has to differ from the native API or otherwise it wouldn't be a library and would overwrite the existing API which is certainly terrible practice.

    Normally I don't advocate the use of libraries, especially for those who couldn't duplicate similar functionality without the library, but you cannot fault jQuery for Prototypes mistakes.

    some of the reasons I advise people not use these [i]so called[/i] api's are because[/quote](emphasis added)

    They are not "so called", they [b]are[/b] libraries, and their bad points do not diminish the inexcusable fact that they [b]are[/b] libraries.
    Γ—

    Success!

    Help @jasmine123 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 β€” BETA 5.4,
    whats_new: community page,
    up_next: more Davinciβ€’003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinciβ€’003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...