[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

run 2 javascript functions on the same page

Posted on 2009-07-07
3
Medium Priority
?
320 Views
Last Modified: 2012-05-07
Hi all,
I have 2 javascripts which are working fine.  On script shows/hides content depending on a checkbox tick.  The other script does an auto complete suggestion select box for a textarea.  When I put both scripts on the same page, they both work without errors except my body onload stops working.  The body onload hides the show/hide content by default.  If I tick the check box to show the already shown content, then untick it, it hides the content just fine.
When I comment out the function for the autocomplete script, the content becomes hidden by default, but of course the suggestion select box script stops working.
Here is what I have:
/* This is the auto complete script */
<script type="text/javascript">
<!--
if (location.href.indexOf('<?=$url;?>') != -1) {
window.onload = function ()
// if I comment out the above line, the show/hide script works onload
{
AutoComplete_Create('ship', ['FedEx', 'UPS', 'USPS', 'DHL', 'Freight', 'ups', 'usps'].sort(), 6);
}
}
if (top.location.href != location.href) {
top.location.href = location.href;
}
//-->
</script>
 
/* This is the show/hide script */
<script type="text/javascript">
<!--
function showMe (it, box) {
var vis = (box.checked) ? "block" : "none";
document.getElementById(it).style.display = vis;
}
//-->
</script>
...
 
/* This is the body onload part */
<body onLoad="showMe('div1', this)">

Open in new window

0
Comment
Question by:newbe101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 2000 total points
ID: 24799446
because both are trying to take over the onload

Perhaps something like:



/* This is the auto complete script */
<script type="text/javascript">
<!--
  if (location.href.indexOf('<?=$url;?>') != -1) {
    var oldOnload = window.onload;
    window.onload = function ()
// if I comment out the above line, the show/hide script works onload
    {
      if ( oldOnload ) {
        oldOnload();
      }
      AutoComplete_Create('ship', ['FedEx', 'UPS', 'USPS', 'DHL', 'Freight', 'ups', 'usps'].sort(), 6);
    }
  }
  if (top.location.href != location.href) {
    top.location.href = location.href;
  }
//-->
</script>
 
/* This is the show/hide script */
<script type="text/javascript">
<!--
  function showMe (it, box) {
    var vis = (box.checked) ? "block" : "none";
    document.getElementById(it).style.display = vis;
  }
  var onload1 = window.onload;
  window.onload = function() {
    if ( onload1 ) {
      onload1();
    }
    showMe('div1', this);
  }
//-->
</script>
...
 
/* This is the body onload part */
<body>

Open in new window

0
 

Author Comment

by:newbe101
ID: 24799544
that was it... thank you!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24799768
You are very welcome.

Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

656 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