Solved

Javascript/jQuery if statement not working

Posted on 2011-09-16
5
298 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
ID: 36552838
X is not defined anywhere...

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

Expert Comment

by:gurvinder372
ID: 36553758
make it

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

Author Comment

by:VAN_AL
ID: 36561574
No, sorry non of it worked for me
0
 
LVL 6

Accepted Solution

by:
jjperezaguinaga earned 350 total points
ID: 36561663
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
ID: 36563256
Thanks @jjperezaguinaga, that worked.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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…

840 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