Solved

Conflicts between codes (javascript)

Posted on 2009-05-06
3
282 Views
Last Modified: 2012-05-06
Hi E's, the code in snippet code open a drop-in content box, the code is correct and work like you see in the image I attach.
The problem is when I use another javascript script in that code, have a conflict, and the drop-in content box don't open. The error is, when I call the second script I change the line <body> to  <body onload="goforit()">.
////////////////////////////FUNCTION goforit and other usuful for the second script
<script>

/*
Live Date Script-
© Dynamic Drive (www.dynamicdrive.com)
For full source code, installation instructions, 100's more DHTML scripts, and Terms Of Use,
visit http://www.dynamicdrive.com
*/


var dayarray=new Array("Domingo","Segunda-Feira","Terça-Feira","Quarta-Feira","Quinta-Feira","Sexta-Feira","Sabado")
var montharray=new Array("Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro")

function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn="AM"
if (hours>=12)
dn="PM"

if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//change font size here
var cdate="<small><font color='#3333cc' size='1' face='Arial'><b>"+dayarray[day]+", "+daym+" de "+montharray[month]+" de "+year+" - "+hours+":"+minutes+":"+seconds+"</b></font></small>"
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}

</script>
<span id="clock"></span>
///////////////////////////////////////////////////////////////////////

What I have to do for use both scripts in the same page?

Regards, JC
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script language="JavaScript1.2">
 

// Drop-in content box- By Dynamic Drive

// For full source code and more DHTML scripts, visit http://www.dynamicdrive.com

// This credit MUST stay intact for use
 

var ie=document.all

var dom=document.getElementById

var ns4=document.layers

var calunits=document.layers? "" : "px"
 

var bouncelimit=32 //(must be divisible by 8)

var direction="up"
 

function initbox(){

if (!dom&&!ie&&!ns4)

return

crossobj=(dom)?document.getElementById("dropin").style : ie? document.all.dropin : document.dropin

scroll_top=(ie)? truebody().scrollTop : window.pageYOffset

crossobj.top=scroll_top-250+calunits

crossobj.visibility=(dom||ie)? "visible" : "show"

dropstart=setInterval("dropin()",50)

}
 

function dropin(){

scroll_top=(ie)? truebody().scrollTop : window.pageYOffset

if (parseInt(crossobj.top)<100+scroll_top)

crossobj.top=parseInt(crossobj.top)+40+calunits

else{

clearInterval(dropstart)

bouncestart=setInterval("bouncein()",50)

}

}
 

function bouncein(){

crossobj.top=parseInt(crossobj.top)-bouncelimit+calunits

if (bouncelimit<0)

bouncelimit+=8

bouncelimit=bouncelimit*-1

if (bouncelimit==0){

clearInterval(bouncestart)

}

}
 

function dismissbox(){

if (window.bouncestart) clearInterval(bouncestart)

crossobj.visibility="hidden"

}
 

function truebody(){

return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body

}
 
 

window.onload=initbox
 

</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>
 

<body>

<div id="dropin" style="position:absolute;visibility:hidden;left:200px;top:100px;width:500px;height:300px;background-color:#F5F5F5">
 

<div align="right"><a href="#" onClick="dismissbox();return false">[Close Box] </a></div>
 

SPECIFY YOUR CONTENT HERE. IT COULD BE TEXT, IMAGES, OR RICH HTML
 

</div>

</body>

</html>

Open in new window

onload.gif
0
Comment
Question by:Pedro Chagas
3 Comments
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 320 total points
ID: 24319309
Get rid of the window.onload = initbox statement on line 58

set the onload attribute in the body to "initbox(); goforit();" in order to execute both functions when the document is loaded.
0
 
LVL 10

Assisted Solution

by:bugada
bugada earned 180 total points
ID: 24319673
in a more modern javascript maintain your window.onload, but assign a closure to that event. Change your event handleras follows:

(bytheway this solution is equivalent to AlbertVanHalen one, so give point to him in te case).
window.onload = function(e) {

   initbox(); 

   goforit();

}

Open in new window

0
 
LVL 3

Author Closing Comment

by:Pedro Chagas
ID: 31578672
Two great solutions. Both solutions work well, I give more points for @AlbertVanHalen because he post first.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now