0

I am using javascript function where i need to check undefined value in if condition.

var a = getParameterByName('a');
// if condition comes true only then below line should be executed. 
// but everytime either condition is true or false, below statement is executed.
if (typeof (a) !== "undefined") { 
    document.getElementById("img1").src = getParameterByName('a');
}
if (typeof getParameterByName('d') !== "undefined") {
    document.getElementById("video").src = getParameterByName('d');
}

is there any problem in using 'undefined' ?? please help me as soon as posssible. Thanks

Isbah Khan
  • 115
  • 7
  • 14

2 Answers2

0

it might look like var a = getParameterByName('a'); is getting value a = '';.

In javascript '' != undefined so check type of a.

Sohil Desai
  • 2,940
  • 5
  • 23
  • 35
0

Very inefficient code.

Try this. Src will be changed if a is none of false, empty string, null, undefined or 0 It will not work as expected if a can be 1, true or some string not a known image type. Pragmatically I assume the getParameterByName will return "image.gif" or "image.jpg" or one of the falsy values I described

var a = getParameterByName('a'); 
if (a) document.getElementById("img1").src = a;
mplungjan
  • 169,008
  • 28
  • 173
  • 236