VAN_AL
asked on
Javascript/jQuery if statement not working
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.j s"></scrip t>
<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>
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.j
<script>
var link = $.backstretch("<?php echo get_post_meta($post->ID, 'upload_image', true); ?>")
var altLink = '<?php bloginfo('template_url'); ?>/images/not-set-default.
if (x == link) {
document.write(link)
} else {
document.write(altLink)
}
</script>
make it
if ( x != undefined && link != undefined && x == link)
{
document.write(link)
}
else
{
document.write(altLink)
}
if ( x != undefined && link != undefined && x == link)
{
document.write(link)
}
else
{
document.write(altLink)
}
ASKER
No, sorry non of it worked for me
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks @jjperezaguinaga, that worked.
Try:
if(link){
document.write(link)
}else
...