Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  81] [ 2]  / answers: 1 / hits: 15723  / 13 Years ago, thu, january 12, 2012, 12:00:00

In php I'm writing the following



<?php
if(x != 0) {
echo function myfunction(){};;
}
?>


In javascript I wish to test if the function exists and if not write out an empty function



if(typeof myfunction != 'function'){
function myfunction(){};
}


This works great in firefox but, in chrome even if the typeof equals function it still goes into and creates the empty function. I can't find a work around for chrome, i've tried if(!myfunction), if(typeof window.myfunction != 'function) but nothing seems to work here for chrome when everything seems to work in firefox. Any ideas?


More From » function

 Answers
141

Use a function expression, not a function declaration.



if(typeof myfunction != 'function'){
window.myfunction = function(){};
}


(I'm using window since your last paragraph suggests you want a global function)


[#88051] Wednesday, January 11, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jayla

Total Points: 14
Total Questions: 96
Total Answers: 123

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;