Title: Javascript in if-else function not working
Last modified: August 19, 2016

---

# Javascript in if-else function not working

 *  [diederikmeijer](https://wordpress.org/support/users/diederikmeijer/)
 * (@diederikmeijer)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/javascript-in-if-else-function-not-working/)
 * Hi,
 * I am trying to call a Javascript function from a PHP if-else function. The if-
   else function itself and the else part are working fine, I just can’t get the
   Javascript to run when the if condition is met.
 * I have tested the Javascipt code by dropping it into a plain html page and there
   it runs fine.
 * Would anybode here know what I’m doing wrong??
 * Thank you,
    diederik
 * Here is the code:
 *     ```
       <?php if('the_title'!=="At Random Play") : ?> <?php echo '<div id="flashcontent">
        <strong>XML gallery</strong>
       </div>
       <script type="text/javascript">
        var so = new SWFObject("http://www.theblacksnapper.net/wp-content/uploads/2009/random_play_2009/gallery.swf", "", "620", "503", "8", "#ffffff");
        so.addParam("allowScriptAccess", "always");
        so.addVariable("xmlfile", "http://www.theblacksnapper.net/wp-content/uploads/2009/random_play_2009/xml/images.xml");
        so.write("flashcontent");
       </script>'; ?> <?php else : ?> <?php echo get('part_flash');?> <?php endif; ?>
       ```
   
 * I have the sfwobject in my header like this:
    `<script type="text/javascript"
   src="http://www.theblacksnapper.net/swfobject.js"></script>`

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Ben Sutcliffe](https://wordpress.org/support/users/bsutcliffe/)
 * (@bsutcliffe)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/javascript-in-if-else-function-not-working/#post-1321176)
 * Here are my suggestions:
    1. The `if else` looked a little odd. You had a `!==` which I’ve never heard of…
       🙂
    2. I’ve broken everything in to a few `echo` calls so we can see what’s going on.
    3. When you next call this, can you look to see if there are any JavaScript errors?
       Here’s how you find these in [Internet Explorer](http://www.mediacollege.com/internet/javascript/basic/error.html).
       For Safari go: Page -> Develop -> Show Error Console; Firefox: Tools -> Web 
       Development; and Chrome: Page -> Developer -> JavaScript console.
 * So, here’s some code:
 *     ```
       <?php 
   
       if(the_title()!="At Random Play") { 
   
           echo '<div id="flashcontent"><strong>XML gallery</strong></div>';
           echo '<script type="text/javascript">';
           echo 'var so = new SWFObject("http://www.theblacksnapper.net/wp-content/uploads/2009/random_play_2009/gallery.swf", "", "620", "503", "8", "#ffffff");';
           echo 'so.addParam("allowScriptAccess", "always");';
           echo 'so.addVariable("xmlfile", "http://www.theblacksnapper.net/wp-content/uploads/2009/random_play_2009/xml/images.xml");';
           echo 'so.write("flashcontent");';
           echo '</script>'; 
   
       } else {
   
           echo get('part_flash');
   
       };
   
       ?>
       ```
   
 *  [Supplybrary](https://wordpress.org/support/users/reelvisits/)
 * (@reelvisits)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/javascript-in-if-else-function-not-working/#post-1321645)
 * diederik.meijer,
 * Were you able to get this to work? I’m trying the same thing. If so, will you
   give an example?
 *  [Supplybrary](https://wordpress.org/support/users/reelvisits/)
 * (@reelvisits)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/javascript-in-if-else-function-not-working/#post-1321646)
 * Hey, mine works now. I just needed to add the full URLs.
 *     ```
       function getCustomField()
       {
       	global $post;
   
       	$video = js_escape(get_post_meta($post->ID, 'video', true));
       	$vid_place = js_escape(wp_get_attachment_url($video));
   
       		if($video) : ?>
   
       <script type='text/javascript' src='http://actual/path/to/swfobject.js'></script>
       <p><div id='<?php echo $video;?>'>This text will be replaced</div></p>
       <script type='text/javascript'>
         var so = new SWFObject('http://actual/path/to/player.swf','ply','470','320','9','#ffffff');
         so.addParam('allowfullscreen','true');
         so.addParam('allowscriptaccess','always');
         so.addParam('wmode','transparent');
         so.addVariable('file','<?php echo $vid_place;?>');
         so.write('<?php echo $video;?>');
       </script>
       <?php
   
       endif;
   
       }
       ```
   
 * I got the hint from this post.
 * [http://wordpress.org/support/topic/386748?replies=5](http://wordpress.org/support/topic/386748?replies=5)

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Javascript in if-else function not working’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * 3 replies
 * 3 participants
 * Last reply from: [Supplybrary](https://wordpress.org/support/users/reelvisits/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/javascript-in-if-else-function-not-working/#post-1321646)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
