944,196 Members | Top Members by Rank

Ad:
Nov 23rd, 2006
0

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

Expand 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:

html Syntax (Toggle Plain Text)
  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;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
procyon is offline Offline
2 posts
since Nov 2006
Nov 24th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by procyon ...
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:

html Syntax (Toggle Plain Text)
  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]
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 24th, 2006
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
procyon is offline Offline
2 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Selectively Printing only Certain Divs (javascript/css question)
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Random CSS Stylesheet selector exists?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC