Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript/jQuery if statement not working

Posted on 2011-09-16
5
Medium Priority
?
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Gurvinder Pal Singh
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 1400 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…

597 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