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>