Solved

Javascript/jQuery if statement not working

Posted on 2011-09-16
5
299 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: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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Title # Comments Views Activity
Google Tag Manager - Add Trigger Using Div class 22 64
ASP get fieds value 2 22
Use Mid in Html 6 23
arrays and radio buttons 10 40
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

697 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