Solved

Javascript/jQuery if statement not working

Posted on 2011-09-16
5
293 Views
Last Modified: 2012-05-12
I'm currently using the backstretch.js script for full screen background images, I've got it working but I would like to add an if statement to check if an image is set, which are set via custom post types display that image, if not, the default image should be link to image

This is what I have so far, but it doesn't work and need help fixing it:

<script src="<?php bloginfo('template_url'); ?>/js/jquery.backstretch.js"></script>
    <script>
      var link = $.backstretch("<?php echo get_post_meta($post->ID, 'upload_image', true); ?>")
      var altLink = '<?php bloginfo('template_url'); ?>/images/not-set-default.jpg'
        if (x == link) {
             document.write(link)
        } else {
             document.write(altLink)
        }

    </script>
0
Comment
Question by:VAN_AL
5 Comments
 
LVL 25

Expert Comment

by:Kyle Hamilton
Comment Utility
X is not defined anywhere...

Try:
if(link){
document.write(link)
}else
...
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
make it

if ( x != undefined && link != undefined && x == link)
{
       document.write(link)
}
else
{
       document.write(altLink)
}
0
 
LVL 1

Author Comment

by:VAN_AL
Comment Utility
No, sorry non of it worked for me
0
 
LVL 6

Accepted Solution

by:
jjperezaguinaga earned 350 total points
Comment Utility
Greetings VAN_AL,

I think your approach to solve the problem is not the right one. You are using $.backstretch(...) without checking the image, and the writing on the document the instance of the plugin.

A better approach would be the following one:
	<script src="<?php bloginfo('template_url'); ?>/js/jquery.backstretch.js"></script>
    <script>
	  <?php $image_link = get_post_meta($post->ID, 'upload_image', true); 
	  if (isset($image_link)) { ?>
		$.backstretch("<?=$image_link?>");
	  <?php } else { ?>
		$.backstretch("<?php bloginfo('template_url'); ?>/images/not-set-default.jpg");
	  <?php } ?>
    </script>

Open in new window


Hope it helps.
Best regards,
-JJ
0
 
LVL 1

Author Comment

by:VAN_AL
Comment Utility
Thanks @jjperezaguinaga, that worked.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now