Is the job variable / defined?

I use forum software where you can pull up an avatar link using this: {$ mybb-> user ['avatar']}

If the avatar is not set, nothing is returned. I want to show the image if the avatar is not set.

HTML is currently configured as follows:

    <div id="avatar"><div style="background: url({$mybb->user['avatar']}) center center no-repeat;"></div></div>

The outer div is empty and in the background. This is the one I add to the class (.defaultavatar) so that the default avatar is displayed.

The inner div is the one that displays the avatar. If the avatar is not loaded / selected, it will be displayed empty.

This is the javascript I am using:

    var myavatar = {$mybb->user['avatar']};

    if(typeof(myavatar) = null){ 
        jQuery('#avatar').addClass('defaultavatar');
    }

If the avatar url is empty, I want the defaultavatar class to be added to the first div.

How can I do this (make it work)?

Thanks a lot Jack Clark

0
3

javascript undefined. , , undefined REALLY undefined.

undefined - , , , , . , , , .

if, , , undefined. , '' 0 false. 0, myDeclaredVariable === undefined .

if (myDeclaredVariable) {
    // myDeclaredVariable is initialized with a "truthy" value
} else {
    // myDeclaredVariable is either uninitialized or has a "falsey" value
}

undefined - , , , . , , javascript, - . , typeof operator.

if (typeof myPossiblyDeclaredVariable !== 'undefined') {
    // myPossiblyDeclaredVariable has been declared
} else {
    // myPossiblyDeclaredVariable has not been declared yet
}

StackOverflow .

+1

if , . - .

if(typeof(myavatar) = null)   

if (typeof myavatar == 'undefined')
+2

Depending on what returns {$mybb->user['avatar'], this might be enough:

if (myavatar) {
  // do stuff
}  
+1
source

All Articles