Can't put 2 Javasript working in the same page!

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Nov 2006
Posts: 2
Reputation: procyon is an unknown quantity at this point 
Solved Threads: 0
procyon procyon is offline Offline
Newbie Poster

Can't put 2 Javasript working in the same page!

 
0
  #1
Nov 23rd, 2006
I am working on a website and I dont know much of Javascript,but I need to put to work different scripts on the page,and it wont work! I've already try to chage all that I remeber to chage,but dosen't work!What I am doing wrong?Can anybody help me out? Here is my code:

  1. <script type="text/javascript" src="/js/mootools.js" ></script>
  2. <script type="text/javascript" src="/js/slimbox.js"></script>
  3.  
  4. <script type="text/javascript">
  5. window.onload = function() {
  6. // Pick your classes
  7. var myBox = document.getElementsByClassName('box_title');
  8. var myBoxOpen = document.getElementsByClassName('information');
  9. // Create the accordion
  10. var myAccordion = new fx.Accordion(
  11. myBox, myBoxOpen, {
  12. onActive: function(tog){
  13. tog.setStyles({color: '#fff', background: '#840000'});
  14. },
  15. onBackground: function(tog){
  16. tog.setStyles({color: '#b7d30b', background: '#7d8c87'});
  17. },
  18. alwaysHide: true
  19. }
  20. );
  21. }
  22. function MM_callJS(jsStr) { //v2.0
  23. return eval(jsStr)
  24. }
  25.  
  26. </script>
  27.  
  28. <script type="text/javascript" src="/js/jquery.js"></script>
  29. <script type="text/javascript" src="/js/innerfade.js"></script>
  30.  
  31. <script type="text/javascript">
  32. $(document).ready(
  33. function(){
  34. $('ul#hFade').innerfade({
  35. speed: 7000,
  36. timeout: 13000,
  37. type: 'sequence',
  38. containerheight: 'auto'
  39. });
  40. });
  41. </script>

Thanks for any help;
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Can't put 2 Javasript working in the same page!

 
0
  #2
Nov 24th, 2006
Originally Posted by procyon View Post
I am working on a website and I dont know much of Javascript,but I need to put to work different scripts on the page,and it wont work! I've already try to chage all that I remeber to chage,but dosen't work!What I am doing wrong?Can anybody help me out? Here is my code:

  1. <script type="text/javascript" src="/js/mootools.js" ></script>
  2. <script type="text/javascript" src="/js/slimbox.js"></script>
  3.  
  4. <script type="text/javascript">
  5. window.onload = function() {
  6. // Pick your classes
  7. var myBox = document.getElementsByClassName('box_title');
  8. var myBoxOpen = document.getElementsByClassName('information');
  9. // Create the accordion
  10. var myAccordion = new fx.Accordion(
  11. myBox, myBoxOpen, {
  12. onActive: function(tog){
  13. tog.setStyles({color: '#fff', background: '#840000'});
  14. },
  15. onBackground: function(tog){
  16. tog.setStyles({color: '#b7d30b', background: '#7d8c87'});
  17. },
  18. alwaysHide: true
  19. }
  20. );
  21. }
  22. function MM_callJS(jsStr) { //v2.0
  23. return eval(jsStr)
  24. }
  25.  
  26. </script>
  27.  
  28. <script type="text/javascript" src="/js/jquery.js"></script>
  29. <script type="text/javascript" src="/js/innerfade.js"></script>
  30.  
  31. <script type="text/javascript">
  32. $(document).ready(
  33. function(){
  34. $('ul#hFade').innerfade({
  35. speed: 7000,
  36. timeout: 13000,
  37. type: 'sequence',
  38. containerheight: 'auto'
  39. });
  40. });
  41. </script>

Thanks for any help;
Hi,

It looks like you're using two JS libs, mootools and jquery. I haven't used either but from what you explained I noticed:

$(document).ready(function() { ... });

and

window.onload = function() { ... };

are probably the same thing...

So the second one to be executed would overwrite the first as the window.onload function.

If you want to execute multiple functions on window load you may want to use a funciton like:

[HTML]

window.multi_onload = function(func) {

if (typeof(window.onload) == 'function') {

var load = window.onload;

window.onload = function() {
load();
func();
};

} else {

window.onload = func;

}

};

[/HTML]

This would still fall short if someone were to use html like:

<body onload="init();">

(I believe) since "init();" would have the type "string".

for this maybe a try/catch statement around an eval() of the old window.onload of type "string" would work....

Untested:

[HTML]

window.multi_onload = function(func) {

if (typeof(window.onload) == 'function') {

var load = window.onload;

window.onload = function() {
load();
func();
};

} else if (typeof(window.onload) == 'string') {

var load = window.onload;

window.onload = function() {
try {
eval(load);
func();
} catch(e) {
/* trap error */
}
};

} else {

window.onload = func;

}

};

[/HTML]
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 2
Reputation: procyon is an unknown quantity at this point 
Solved Threads: 0
procyon procyon is offline Offline
Newbie Poster

Re: Can't put 2 Javasript working in the same page!

 
0
  #3
Nov 24th, 2006
Hi,

Thanks a lot for your help! I will try that,but I am not very familiar with JS.Hope I can put it to work. Anyway the two JS libs work great,but I've already try to put like this window.onload = function() { $(document).ready(function() { ... }); }; but dosen't work!!I have another idea,include an external file with just the one script,maybe it works.I will work on it and later I post my results.Thanks. :-)
Last edited by procyon; Nov 24th, 2006 at 6:04 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC