Monday, June 3, 2024
105
rated 0 times [  108] [ 3]  / answers: 1 / hits: 18045  / 13 Years ago, thu, december 15, 2011, 12:00:00

I want my code so that if a specific var exists it will perform an action, else it will be ignored and move along. The problem with my code is, if the specific var does not exist it causes an error, presumably ignoring the remainder of the JavaScript code.



Example



var YouTube=EpicKris;

if ((typeof YouTube) != 'undefined' && YouTube != null) {
document.write('YouTube:' + YouTube);
};

More From » if-statement

 Answers
38

Code:



var YouTube=EpicKris;

if (typeof YouTube!='undefined') {
document.write('YouTube:' + YouTube);
};


Worked out the best method for this, use typeof to check if the var exists. This worked perfectly for me.


[#88535] Wednesday, December 14, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ashli

Total Points: 210
Total Questions: 94
Total Answers: 96

Location: Austria
Member since Sat, Jul 24, 2021
3 Years ago
;