Make function call inside body instead of onLoad

AphX
AphX used Ask the Experts™
on
Hi Experts!

I'm trying to use this code snippet and adjust it to fit my needs:

http://www.tek-tips.com/faqs.cfm?fid=5889

He is using body onload to call the function.

I want to call the function several times inside the body. How can I do that?

Thank you!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You will need to call either createGradientV() or createGradientH() directly.  You can do this from links, input elements etc...
<button type="button" id="some_id" onclick="createGradientV(...);">Create V Gradient</button>
 
<a href="javascript:void(0);" onclick="createGradientH(...)" title="">Create H Gradient</a>

Open in new window

Author

Commented:
Ooh nice!

Can I call the function without any onclick element?

I want the function to be called like this:

<?php
for(some loop){
    <script type="text/javascript" language="javascript">
      createGradientV([255, 255, 255], [255, 255, 0], 1, 40);
</script>
}
?>

Is this possible?
Indeed you can! ->
<?php
for(some loop){
?>
    <script type="text/javascript" language="javascript">
      createGradientV([255, 255, 255], [255, 255, 0], <?php echo $a_var; ?>, 40);
    </script>
<?php
}
?>

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Alternatively, you could build a JS array in PHP and then do the entire loop on the client:
<?php
  $v_json = "[";
  for (some loop){
    $v_json .= "[255, 255, 255],";
  }
  $v_json = trim($v_json, ",")."]";
?>
 
/* JS in HEAD or in BODY or wherever */
<script type="text/javascript">
  var a_data = <?php echo $v_json; ?>;
  for (var i = 0; i < a_data.length; i++){
    createGradientV(a_data[i], [255, 255, 0], 1, 40);
  }
</script>

Open in new window

Author

Commented:
You are the king! How would I survive without you?! :D
Gracias!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial