开发者

How to run javascript code in any part of the html file?

I am using a jQuery notification system who runs when the user click on a link:

<a href="javascript:void(0);" onclick="$.n('This is a sample notification message, there are 3 more notification types which is all customizable through CSS.');">here</a>

But the thing is that i want to run $.n('Th开发者_如何学Cis is a sample notification') not by pressing a link. I want to call the event when something happend with a If statement made on php.

Ex.

<?php if (condition) { DO THE NOTIFICATION; } ?>


First of all keep in mind that your php code is evaluated on the server, while JavaScript and jQuery run in the browser. The evaluations happen in different times, at different places. Therefore you cannot call a JavaScript function from php.

However with php you can render HTML and JavaScript code such that it is only rendered when your php condition is true. Maybe you may want to try something like this:

<?php 
if (condition) { 
   echo "<script>";
   echo "$.n('This is a sample notification message');";
   echo "</script>";
} 
?>


<?php if (condition){?>
<script>
$.n('This is a sample notification message, there are 3 more notification types which is all customizable through CSS.');
</script>
<?php } ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜