Does JavaScript support function detection?
Like if (this_function()) {alert('function exists!');}?
JavaScript Function detection?
Moderator: General Moderators
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: JavaScript Function detection?
Usually you can just do:
Code: Select all
if(this_function)Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: JavaScript Function detection?
AH! Ok! I was adding the parenthesis like if (function_1())...the following code is not my style but it works. Thanks!
Code: Select all
<script>function hello() {alert('hello');}function test() {if (hello) {alert('exists');} else {alert('not exist');}}</script> <button onclick="test()">sutff</button>Re: JavaScript Function detection?
It's not about anyone's style:
func() - a call to a function
func - a pointer to a function (its address)
pickle's code won't work in IE (mine is ver. 8 ) - it will pop up a JS error message box if hello() doesn't exist.
[js]function a(){} if (typeof(a) == 'undefined') alert('undefined');else alert('defined');[/js]
func() - a call to a function
func - a pointer to a function (its address)
pickle's code won't work in IE (mine is ver. 8 ) - it will pop up a JS error message box if hello() doesn't exist.
[js]function a(){} if (typeof(a) == 'undefined') alert('undefined');else alert('defined');[/js]
There are 10 types of people in this world, those who understand binary and those who don't
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: JavaScript Function detection?
Vlad, the code put in to functions works just fine in IE6 and IE8 (both system not standalones, IE6 is in Virtual PC and IE8 system IE on XP)...
...the reason for this is I will do function detection for user JavaScript functions at some point in Version 2.9 of my site. My site's functions will be very slightly altered to let users adjust those functions by creating "user_" prefix named functions. That way people can not only suggest improvements but make scripts as well as show if their scripts work better. 
Code: Select all
<script>function a(){} function b(){if (typeof(a) == 'undefined') alert('undefined');else alert('defined');}</script> <button onclick="b()">sutff</button>