最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript within php - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Dec 3, 2009 at 22:45 MileeMilee 2231 gold badge4 silver badges13 bronze badges 2
  • 5 You really should learn the very principle of PHP before you start using it to create popup ads... – Joel Commented Dec 3, 2009 at 23:07
  • There is nothing wrong with his PHP other than it being messy. He should, however, acquaint himself more with JavaScript before continuing. – Justin Johnson Commented Dec 4, 2009 at 0:02
Add a ment  | 

3 Answers 3

Reset to default 8

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)"; 
}
?>" >

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

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

Edit: I pletely 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 plex 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>

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 mands 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>
发布评论

评论列表(0)

  1. 暂无评论