views:

164

answers:

3

Hi,

The following code does not work for me

<body onload = "<?php 
foreach($arr as $a){
echo "<script language = javascript> popup_show(\''.$a.'\', \'popup_drag\', \'popup_exit\', \'screen-top-left\', 20,  20) <script>"; 
}
?>" >

I even tried this with an alert .

<body onload = "<?php 
foreach($arr as $a){
echo "<script language = javascript> alert('Hello') <script>"; 
}
?>" >

Even this did not work.

I need to be able to call the popup_show function for all the values of the array$arr.

Thanks for any help. I would really appreciate it.

+8  A: 

When putting Javascript code inside an event handler (eg onload=""), you do not need to include HTML script tags.

So you just need:

<body onload = "popupshow(...)">

Your full example would be:

<body onload = "<?php 
foreach($arr as $a){
  echo "popup_show(\''.$a.'\', \'popup_drag\', \'popup_exit\', \'screen-top-left\', 20,  20)"; 
}
?>" >
Nick
This is likely the issue. Your output is something like <body onload="<script language=javascript> alert('Hello') </script>" > which is invalid. Remove the script tags and it should work.
Adam Raney
This will fail since you are not ending `popup_show(...)` with a semicolon. What you are generating is `popup_show(...)popup_show(...)popup_show(...)popup_show(...)popup_show(...)`
Justin Johnson
+4  A: 

It fails because it is not valid HTML when it finishes being output:

<body onload="<script language = javascript> alert('Hello') <script>">

Edit: I completely revamped my answer based on encouragement from @Justin Johnson

When using PHP to work with JavaScript, I try to move to native JavaScript as soon as possible. Rather than trying to echo complex JS statements from PHP, try passing the data to JS and letting it do the work.

Additionally, it is considered bad form to use the HTML event attributes (onload, 'onclick', etc). It is better to attach your event to the object using IE's attachEvent and the W3's addEventListener.

<script type="text/javascript">
   // Store PHP array as JavaScript array in JS variable
   var messages = <?php echo json_encode($arr) ?>; // echos ["...","..."] etc.

   function trigger_popups(){
     for(var i = 0; i < messages.length; i++){
       popup_show( messages[i], 'popup_drag', 'popup_exit', 'screen-top-left', 20,  20);
     }
   }

   if(window.attachEvent)
      window.attachEvent('onload', trigger_popups); // IE
   else
      window.addEventListener('load', trigger_popups, true); // Other Browsers
</script>
Doug Neiner
These are bad practices
Justin Johnson
Didn't see a jQuery tag on this post.. I don't think a lesson in cross browser event handling was appropriate for this question.
Doug Neiner
You can still attach events properly without a library.
Justin Johnson
Right, with `attachEvent` or `addEventListener` since `window.onload =` is also bad practice.
Doug Neiner
I completely agree.
Justin Johnson
I am torn between agreeing with you (that I should have shown the "right" way) and thinking that a level of detail like that would have been distracting to the answer of the question.
Doug Neiner
I had that same dilemma about a month ago as well. To me, SO is a place to learn and part of that is learning how to do it right. While it is easier and shorter to explain how to attach an event through an HTML attribute, it only results in more of the same questions and more bad programming to come. I recognize that the OP is obviously a novice, but his/her understanding will continue to be lacking if we always cater to the lowest common denominator.
Justin Johnson
Thanks for the encouragement @Justin. Does my new answer look better?
Doug Neiner
Almost ;) `addEventListener` requires 3 parameters (passing `true` is typically fine). Also, you shouldn't use `onload` as the method name, as this is the same as `window.onload` which results in the function that you are defining being called twice.
Justin Johnson
OK ok... I confess. I do code straight JS, but more often than not I use jQuery to do my event handling. Shame on me :) Thanks for the feedback, it should be all set now!
Doug Neiner
I too assumed that addEventListener would have accepted just 2 parameters and it could default the last to false, but I guess that is not the case. Anyway, +1 ;) One thing to be aware of though is http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html which is why using a library or a more robust event attachment method is pretty important (but in this case it is not an issue and I wont beleaguer the point).
Justin Johnson
+1  A: 

You're building invalid HTML. You can't place a script element (or any element, actually) into the attribute of another HTML element (in this case, body).

<!-- This is not valid! -->
<body onload = "<script language = javascript> alert('Hello') <script>">

Event attributes take just javascript only. So, you could do something like this

<body onload = "alert('Hello')">

And that would work

Since it looks like you want to build JavaScript commands from an array of data, may I suggest this approach

<html>
<head>
  <script type="text/javascript">

  onload = function()
  {
  <?php 
    foreach($arr as $a)
    {
      echo "    popup_show('$a', 'popup_drag', 'popup_exit', 'screen-top-left', 20,  20);\n"; 
    }
  ?>
  }
  </script>
</head>
<body>
</body>
</html>
Peter Bailey
But how do I pass the $arr array from the body to the javascript function ?
Milee