PHP:调用Javascript命令

PHP:调用Javascript命令

问题描述:

I'm using a plugin called Reveal, and trying to fire it based on whether a variable is present in the URL (i.e. www.mysite.com?status=new). I have tested the GET status code (works fine, echoed it) and also set up reveal to work when a button is pressed. Both work flawlessly. However, the goal is to fire the event if the status == new. This is my code:

<?
if($status == 'new'){
        echo '<script type="text/javascript">$(\'#myModal\').reveal();
</script>';
}
?>

Doesn't work :/ Any help? I've tried many combinations with this and nothing seems to be working. My code is placed near the bottom of the page (in the body section), not sure if that matters..

EDIT: Also tried this (didn't work):

<?
if($status == 'new'){
        echo '<script type="text/javascript">
        $(document).ready(function() {
          e.preventDefault();
      $(\'#myModal\').reveal();
     });
</script>';
}
?>

Zach

我正在使用名为显示,并根据URL中是否存在变量(即www.mysite.com?status=new)尝试触发它。 我已经测试了GET状态代码(工作正常,回显它),并且在按下按钮时设置显示工作。 两者都完美无缺。 但是,目标是在状态== new时触发事件。 这是我的代码: p>

 &lt;?
if($ status =='new'){
 echo'&lt; script type =“text / javascript”&gt;  $(\ '#myModal \')揭示(); 
&LT; /脚本&GT;'; \ N} 
&GT;吗?
 代码>  PRE> 
 
 

不 工作:/任何帮助? 我尝试了很多这种组合,似乎没有任何效果。 我的代码放在页面底部附近(在正文部分),不确定是否重要.. p>

编辑:还试过这个(没用): p >

 &lt;?
if($ status =='new'){
 echo'&lt; script type =“text / javascript”&gt; 
 $(document).ready  (function(){
 e.preventDefault(); 
 $(\'#myModal \')。reveal(); 
}); 
&lt; / script&gt;'; 
} 
?&gt;  
  code>  pre> 
 
 

Zach p> div>

Try this:

echo '
<script type="text/javascript">
$(document).ready(function(){
    $(\'#myModal\').reveal();
});
</script>';

as m90 already suggested, probably a good idea to load up the DOM before you execute the script. And you can place it anywhere, usually I place it in the head though, but it's all good.

Be aware that this kind of inline JS will be executed immediately by the browser, so it might be that the element you are trying to reveal isn't ready to be revealed yet (therefore nothing happens). Usually you will fix this by calling your scripts on $(document).ready(). See this article on the jQuery website

just wanted to put here a little bit different approach:

// Example URL:
// test.php?status=Message

var $_GET = <?php echo json_encode( $_GET ); ?>;

// alert( $_GET['status'] );
// console.log($_GET);