Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 488 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is variable set/defined issue?

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

If no avatar is set then nothing is returned. I want to show an image if no avatar is set.

Currently the HTML is setup like this:

<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 one is the one I'm adding a class too (.defaultavatar) so that a default avatar will display.

The inner div is the one with the avatar displaying. If no avatar is uploaded/selected then it will appear blank.

This is the javascript I'm using:

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

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

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

How can I do this (get it to work)?

Thanks very much,
Jack Clarke
Reply

#2
in your `if` condition, the syntax has a problem. Single equal is assignment.

at least change this

if(typeof(myavatar) = null)

to

if (typeof myavatar == 'undefined')


Reply

#3
Depending on what `{$mybb->user['avatar']` returns, it may be sufficient to do:

if (myavatar) {
// do stuff
}
Reply

#4
There are two meanings for `undefined` in javascript. I'm not sure what their exact names are, for my example I will call them undefined and REALLY undefined.

The regular `undefined` is what you get when you try to get a value from something that has been declared but not initialized. In other words, the interpreter knows the thing you're talking about, but that thing has no value yet.

You can use a simple if statement to cast your variable to a boolean to check if it is this type of `undefined`. Be aware that this cast will also cast empty strings `''` and numeric `0` to `false` as well. If you need to consider empty strings and numeric `0` as well, use `myDeclaredVariable === undefined` for the condition instead.

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

The REALLY `undefined` is what you get when you try to access a random variable without ever even declaring it. The interpreter has no idea what you are talking about and it will throw a javascript exception if you try to do anything with it. To handle this situation, you need to use the [`typeof` operator][1].

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

You can see a related StackOverflow question [here][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through