在PHP中运行JavaScript然后发生Uncaught SyntaxError

时间:2017-09-29 07:04:08

标签: javascript php jquery

当我在没有PHP的情况下运行它时,运行良好。

      <?php 

       $it="$getuser[active]"; if($it==1)
       { echo '<script type="text/javascript">';
       echo ' $(document).ready(function () {
       var unique_id = $.gritter.add({
        // (string | mandatory) the heading of the notification
        title: "Welcome to my website!",
        // (string | mandatory) the text inside the notification
        text: "Activate Your Account Now!!<a href="active.php" target="_blank" style="color:#ffd777">Click Here</a>.",
        // (string | optional) the image to display on the left
        image: "img.jpg",
        // (bool | optional) if you want it to fade out on its own or just 
        sit there
        sticky: true,
        // (int | optional) the time you want it to be alive for before 
        fading out
        time: "",
        // (string | optional) the class name you want to apply to that specific message
        class_name: "my-sticky-class"
    });';

       echo '  return false;
    });';
    echo '</script> ';
   }
   ?>

1 个答案:

答案 0 :(得分:1)

你必须逃离"text: "Activate Your Account Now!!<a href="active.php" target="_blank" style="color:#ffd777">Click Here</a>.",此行会产生问题,也会在“坐在那里”和“淡出”之前删除换行符

<?php 
$it="$getuser[active]"; if($it==1)
{ 
     echo '<script type="text/javascript">';
     echo ' $(document).ready(function () {
     var unique_id = $.gritter.add({
      // (string | mandatory) the heading of the notification
      title: "Welcome to my website!",
      // (string | mandatory) the text inside the notification
      text: "Activate Your Account Now!!<a href=\"active.php\" target=\"_blank\" style=\"color:#ffd777\">Click Here</a>.",
      // (string | optional) the image to display on the left
      image: "img.jpg",
      // (bool | optional) if you want it to fade out on its own or just  sit there
      sticky: true,
      // (int | optional) the time you want it to be alive for before fading out
      time: "",
      // (string | optional) the class name you want to apply to that specific message
      class_name: "my-sticky-class"
  });';

     echo '  return false;
  });';
  echo '</script> ';
 }
 ?>

修改

您也可以跳过php部分直接编写脚本

<?php 
$it="$getuser[active]"; if($it==1)
{ ?>
    <script type="text/javascript">
     $(document).ready(function () {
     var unique_id = $.gritter.add({
      // (string | mandatory) the heading of the notification
      title: "Welcome to my website!",
      // (string | mandatory) the text inside the notification
      text: 'Activate Your Account Now!!<a href="active.php" target="_blank" style="color:#ffd777">Click Here</a>.',
      // (string | optional) the image to display on the left
      image: "img.jpg",
      // (bool | optional) if you want it to fade out on its own or just  sit there
      sticky: true,
      // (int | optional) the time you want it to be alive for before fading out
      time: "",
      // (string | optional) the class name you want to apply to that specific message
      class_name: "my-sticky-class"
  });

     return false;
  });
  </script>
 <?php }
 ?>