PDA

View Full Version : Multiple applets in same page


ilansa
09-12-2002, 04:36 AM
Im having trouble building a page that contains 2 js applets.

basically, one is a fading slideshow with the script declared

between the <head> tags:

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>


<SCRIPT LANGUAGE="JavaScript">
<!-- Original: CodeLifter.com (support@codelifter.com) -->
<!-- Web Site: http://www.codelifter.com -->
<!-- Begin
// Set slideShowSpeed (milliseconds)
var slideShowSpeed = 2000;
// Duration of crossfade (seconds)
var crossFadeDuration = 3;
// Specify the image files
var Pic = new Array();
// to add more images, just continue
// the pattern, adding to the array below

Pic[0] = 'man.jpg'
Pic[1] = 'woman.jpg'
Pic[2] = 'kid.jpg'


// do not edit anything below this line
var t;
var j = 0;
var p = Pic.length;
var preLoad = new Array();
for (i = 0; i < p; i++) {
preLoad[i] = new Image();
preLoad[i].src = Pic[i];
}
function runSlideShow() {
if (document.all) {
document.images.SlideShow.style.filter="blendTrans(duration=2)";
document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration)";
document.images.SlideShow.filters.blendTrans.Apply();
}
document.images.SlideShow.src = preLoad[j].src;
if (document.all) {
document.images.SlideShow.filters.blendTrans.Play();
}
j = j + 1;
if (j > (p - 1)) j = 0;
t = setTimeout('runSlideShow()', slideShowSpeed);
}
// End -->
</script>

</head>

<body onLoad="runSlideShow()">



The other is a dhtml menu that is declared in the body:

<body onLoad="runSlideShow()">



<!-- DHTML QuickMenu, Copyright (c) 2001, OpenCube Inc. All Rights Reserved - www.opencube.com -->
<script language="JavaScript1.2" src="sample_data.js"></script>
<script language="JavaScript1.2" src="dqm_script.js"></script>
<!--end copy above this line-->


The problem is, I can only run one of these scripts at any given time. They will not both work at the same time. Any ideas??[B]

Spookster
09-12-2002, 06:15 AM
First of all do not confuse Java and Javascript. They are two different things. There are Java Applets and Javascripts but no Javascript Applets. What you have there is just Javascripts not applets.

Without seeing the other scripts no definitive answer can be given. Most like though it is one of two things either conflicting variables or a conflict with multiple onload events. This problem and solution are covered in this tutorial:

http://javascriptkit.com/javatutors/multiplejava.shtml

ilansa
09-12-2002, 01:07 PM
Thanks for your reply. I understood the explanation, however, the problem here is a bit more complex. My second javascript is called from an external source file
<script language="JavaScript1.2" src="sample_data.js"></script>
<script language="JavaScript1.2" src="dqm_script.js"></script>

The above referenced javascript is for the drop down menu. The second onload statement is referenced in dqm_script.js. There is another file where i can set parameters, such as an additional onload if I have one, but after setting it, i get an error:preload...source is null. So, the main problem here is that my first javascript (fading slideshow) requires the insertion of an onload statement directly in the body tag, but it wont work since the second onload is referenced after the first one is. If you can help here, i would greatly appreciate it!!!

ilansa
09-12-2002, 05:35 PM
Anybody?

Spookster
09-12-2002, 06:21 PM
Still can't really help you without seeing the other script with the onload event handler. Essentially what you will need to do is to combine all instances of those onload event handlers into one. You can do that one of two ways. First way is by putting both into the onload statement in the body tag:

<body onLoad="runSlideShow();function2()">

function2 being the function of command being executed in the other external script.

Or create a small script in the head of your document like so:

<script language="Javascript">
<!--//

function init(){

runSlideShow();
function2();

}

window.onload=init;

//-->

</script>

You just create a function that lists all the other functions of your other scripts that require the onload event handler to start them. Then you just use one onload event handler to start the function you just created.

This was also explained in the tutorial I posted a link to.

ilansa
09-12-2002, 06:38 PM
Here is the code from dqm_script.js:

q18=0;q19=new Array();dh=DQM_divider_height;q61 =false;ns=navigator.appName==("Netscape");ns4=(ns &&(navigator.appVersion.indexOf("4.")!=-1));mac=(navigator.appVersion.indexOf("Mac")!=-1);ie=(navigator.appName.toLowerCase().indexOf("microsoft")>-1);ie5=(navigator.appVersion.toLowerCase().indexOf("msie 5.")!=-1);ie6=(navigator.appVersion.toLowerCase().indexOf("msie 6.")!=-1);ie4=((ie)&&(!ie5)&&(!ie6));ns5=(navigator.vendor==("Netscape6")|| navigator.product==("Gecko"));if((ns5)||(ie6)||(ie4))ie5=true;ie5only=((ie5)&&(!ns5)&&(!mac));(ie5only)? brn="ie":brn="ns";if((ns5)||((ie5)&&(mac)))brn="ns6";document.write("<script language=\"JavaScript1.2\" src=\""+DQM_codebase+"dqm_"+brn+".js\"></script>");document.close();;function DQM_handleOnload(){if(q61)return;q31();if(!ie5only){i=0;while(eval("window.DQM_rollover_image"+i)){q20=q20.concat(new Array(new Image()));q20[i].src=eval("DQM_rollover_image"+i);q21=q21.concat(new Array(q16("menu"+i).src));i++;}}q61=true;eval(DQM_onload_statement);};function q27(nid){if(eval("window.DQM_rollover_image"+nid)){if(ie5only)eval("q16(qmim"+nid+")."+q59);else q16("menu"+nid).src=q21[nid];}};function q30(menu){if((menu.lasthl!=null)&&(menu.q60==null))q4(menu);};function q31(){for(i=0;i<q18;i++){if(eval("window.DQM_subdesc"+i+"_0"))q1(i,true);}for(i=0;i<q19.length;i++)q1(q19[i],false);};function q32(index){if(!eval("window.DQM_url"+index))return;if(eval("window.DQM_urltarget"+index))where=eval("DQM_urltarget"+index);else where=DQM_urltarget;if((where.length<1)||(where.toLowerCase()=="_self")){window.location.href=eval("DQM_url"+index);}else {if(where.toLowerCase().indexOf("_parent")>-1)eval("parent.window.location=DQM_url"+index);else if(where.toLowerCase().indexOf("_new")<0)eval("parent."+where+".location=DQM_url"+index);else window.open(eval("DQM_url"+index));}};function q33(coords){var q72=new Array(0,0);var txy=coords.indexOf(",");if(txy!=-1){q72[0]=parseInt(coords.substring(0,txy));q72[1]=parseInt(coords.substring(txy+1));}return q72;};function getLevelVal(id,endv,rt){level=0;k=0;while((k=id.indexOf("_",k+1))>-1)level++;if(eval("window.DQM_border"+endv+level))return eval("DQM_border"+endv+level);else return rt;}

Spookster
09-13-2002, 02:57 AM
Well there is not an onload event handler in that script unless i'm going blind. The onload event handler will look just like this:

window.onload=

It might be better if you can post a link to your page with all this so we can see it all in action. It would be easier to debug that way.

Of course on another note does your script look all jumbled like that in the external script file you have? If that is the case then that would be the reason why that script isn't working. It needs to be formatted properly.

ilansa
09-13-2002, 03:49 AM
If you copy the script to notepad and do a search you will find an onload in there. The script was downloaded from www.opencube.com, so I didnt write it. I agree its jumbled and messy but thats beyond my control and has only added to my frustration. The slideshow will only work when the onload is inserted directly in the body tag, and I cant figure out how to combine it with the onload for the dropdown menu, since it uses a js include and i dont know how to isolate the onload handle from it.

Spookster
09-13-2002, 05:27 AM
For everyone else this issue is being partially discussed in PM's due to the fact that the URL cannot be posted in here at this time.

So far the menu script is not the problem nor does the menu actually use a window.onload event handler so that is not the problem.

The problem will either be a variable conflict among the two scripts or just a simple typo in the slideshow script.

Spookster
09-13-2002, 05:49 AM
Ok after debugging the problem indicates a variable conflict between the two scripts. I made a few adjustments to the slideshow script so copy and paste this in place of what you have:



<SCRIPT LANGUAGE="JavaScript">
<!-- Original: CodeLifter.com (support@codelifter.com) -->
<!-- Web Site: http://www.codelifter.com -->
<!-- Begin
// Set slideShowSpeed (milliseconds)
var slideShowSpeed = 2000;
// Duration of crossfade (seconds)
var crossFadeDuration = 3;
// Specify the image files
var Pic = new Array();
// to add more images, just continue
// the pattern, adding to the array below

Pic[0] = 'man.jpg'
Pic[1] = 'woman.jpg'
Pic[2] = 'kid.jpg'


// do not edit anything below this line
var t1;
var j1 = 0;
var p1 = Pic.length;
var preLoad = new Array();
for (i = 0; i < p1; i++) {
preLoad[i] = new Image();
preLoad[i].src = Pic[i];
}
function runSlideShow() {
if (document.all) {
document.images.SlideShow.style.filter="blendTrans(duration=2)";
document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration)";
document.images.SlideShow.filters.blendTrans.Apply();
}
document.images.SlideShow.src = preLoad[j1].src;
if (document.all) {
document.images.SlideShow.filters.blendTrans.Play();
}
j1 = j1 + 1;
if (j1 > (p1 - 1)) j1 = 0;
t1 = setTimeout('runSlideShow()', slideShowSpeed);
}
// End -->
</script>

Spookster
09-13-2002, 06:18 AM
Ok the problem has been resolved. It was a variable conflict. People that write scripts using variable names like k, j, t or p or other names that are common should not do that but instead use odd names even if it looks funny it will reduce the occurances of problems of this nature. For people like ilansa that download scripts and then they don't work because of something so trivial that just makes the author of the script look bad.