Portal Home > Knowledgebase > Articles Database > js scripts


js scripts




Posted by raulgonzalez, 05-30-2007, 07:26 PM
Hello, I have two scripts that I grabed from DHTML Drive and put each one in its respective file. Then I call them both from a php script because I want to use both of them in the same page. It seems that they bump into each other because they both work by themselves, but together they don't. How do I prevent this?

Posted by mwatkins, 05-30-2007, 07:36 PM
That's rather like saying to someone over the phone "I let my 3 year old tie a bunch of string into a gargantuan knot -- how do I untie it?" Well, not quite like that, but almost. It certainly would be helpful if you try to diagnose the problem and report your findings, any findings other than "it doesn't work". So, the diagnostic mind looks at this and starts to ask questions, questions like: - Do both JS scripts fail when included in a page together, or just one? - Does it happen to be the last script referenced that runs, or fail? A common point of failure for those who are almost literally trying to just cut and paste code into a page in the hopes that it will work is that, typically, many scripts have an initialization that must be run when the page loads. Hint, look for onload statements in or for one script that might be clobbering the other. If that is indeed your case, google will find you various solutions for initializing more than one routine on a page load.

Posted by HostSentry, 05-30-2007, 08:11 PM
A link to the page would be nice.

Posted by yasbas, 05-31-2007, 08:08 AM
Any javascript error message from the browser ?

Posted by raulgonzalez, 05-31-2007, 09:12 AM
I apologize for the lack of information. Below are the two codes that when run togheter the CODE 1 will not work. Now I am not all there lost. I know they both have something in common that might prevent them from working both. I just thought I could wrap them into a PHP function or something rather then trying to untide a 3 year old mess. I am not an expert in javascritps but I like to take advantage of open source software. I promise you that I am not just copying, pasting and hoping . The way I made it work for now is, instead of including both of them form my php code I included only one, and the other is being called from a borderless iframe. I can live with it, but I will be searching for a better method as per the javascript error message from the browser, no there is no error at all. Well thanks SyndicateTech and Yasbas The live page is here: http://www.laredo.edu/LearningCenter it is the in the middle content, one of them is the very first TICKER and the other one is the folling SLIDESHOW below the ticker. below are the two codes ///////////////////////////////BEGINS CODE 1///////////////////// ///////////////////////////////ENDS CODE 1///////////////////// ///////////////////////////////BEGINS CODE 2///////////////////// ///////////////////////////////ENDS CODE 2///////////////////// Last edited by raulgonzalez; 05-31-2007 at 09:19 AM.

Posted by mwatkins, 05-31-2007, 09:50 AM
As I suspected, both scripts are trying to assign the "window.onload" to different functions. "populate" for one script, "Play" for the other. This is a very common problem -- when you have more than one js action that you want to deliver to the user when a page loads, you need to find a way deal with initializing more than one event handler. A simple way is to create a function which calls the handlers: And then assign that function as the new window.onload handler. There are other approaches; but try this first. edit: By the way, I did not review the scripts overall -- there may be other issues lurking there, but the two assignments to window.onload certainly were problems that need addressing first. Last edited by mwatkins; 05-31-2007 at 10:00 AM.

Posted by nnormal, 05-31-2007, 12:16 PM
this would be a good place to start... http://www.getfirebug.com/

Posted by mwatkins, 05-31-2007, 12:25 PM
And an entry level Javascript book plus a programming or QA 101 level book or course. The problem often is not lack of knowledge, but more a lack of understanding as to how to break down a problem and look for logical courses of action to follow up on. People need to develop a habit of diagnostic thinking, i.e.: Problem: no output Logically then, we first test to see if the code is even running. Hey, we don't get far in this problem before already stumbling upon a symptom which will lead to a cure. Doing a google search on javascript + tutorial returns a number of links. Maybe run through a few of those before diving in deeper. JavaScript Tutorial http://www.w3schools.com/js/default.asp Java Script Tutorial for the Total Non-programmer Provides an overview of the language and includes scripting examples. http://www.webteacher.com/javascript/ Javascript Tutorial / WebDeveloper.com ® http://www.wdvl.com/Authoring/JavaScript/Tutorial/



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read