0

I've run into some situations where I need to add conditional statements from PHP functions to javascript items.

Here's an example code:

    <script type="text/javascript">
$(window).bind("load", function() {
$("div.mejs-button.mejs-playpause-button.mejs-play > button").trigger('click');
});
    </script>

And I need to wrap this in a conditional statement:

if (bp_current_user_can( 'bp_moderate' ) || $loggedinuserid == $userid ) {
//conditional statement for above javascript
}

I thought about this for a while, and eventually stumbled on an 'online script converter' site that converts the javascript into php echoes. So now my code is the following:

<?php
if (bp_current_user_can( 'bp_moderate' ) || $loggedinuserid == $userid ) {
echo "    <script type=\"text/javascript\">\n"; 
echo "$(window).bind(\"load\", function() {\n"; 
echo "   $(\"div.mejs-button.mejs-playpause-button.mejs-play >  button\").trigger('click');\n"; 
echo "});\n"; 
echo "</script>\n";
}
?>

And this works as I want it too, but it seems rather messy- especially as I try to apply this with bigger pieces of javascript code, and if I'm wanting to edit the javascript code later on. Is this my only option for wrapping PHP conditional statements in javascript?

3 Answers 3

1

You can do:

<?php if (bp_current_user_can( 'bp_moderate' ) || $loggedinuserid == $userid ):?>
<script type="text/javascript">
   $(window).bind("load", function() {
   $("div.mejs-button.mejs-playpause-button.mejs-play > button").trigger('click');
   });
</script>
<?php endif; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe I didn't think to do this! Thank you
1

Just exit PHP mode and output your js directly..

<?php
if (bp_current_user_can( 'bp_moderate' ) || $loggedinuserid == $userid ) { 
//exit PHP to output pure HTML or JS
?>
<script type="text/javascript">
$(window).bind("load", function() {
$("div.mejs-button.mejs-playpause-button.mejs-play >
 button").trigger('click');
});
</script>
<?php } // back into PHP mode for closing curly

Comments

0

Another option for embedding scripts in PHP is to use a heredoc, whiich looks like this...

$scripts = <<<CODE
<script type="text/javascript">
$(window).bind("load", function() {
$("div.mejs-button.mejs-playpause-button.mejs-play >
 button").trigger('click');
});
</script>
CODE;

Then you can just echo the variable. if(something()) echo $scripts;

Note that you must put the closing CODE; right up against the margin. If you have whitespace before it it will break the heredoc.

..yet anotehr way is to use the output buffer

ob_start();
?>
<script type="text/javascript">
$(window).bind("load", function() {
$("div.mejs-button.mejs-playpause-button.mejs-play >
 button").trigger('click');
});
</script>
?>
$scripts = ob_get_clean();

then echo it the same way.. if(something()) echo $scripts;

1 Comment

Interesting. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.