Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function is defined, but Error says.. Function is not found ! (Strange)

This is my code :

function mark()
{
    alert("This is a test box..");
}

setTimeout("mark()",5000);

Error : Function mark() is not found !!

There is some other issue.. as it works on http://jsfiddle.net/russcam/6EXa9/ but its not working in my application.. so can you help me debug this ?

What else can be the reason.. By the way I am running this inside a GreaseMonkey script !

like image 463
Yugal Jindle Avatar asked May 07 '11 07:05

Yugal Jindle


People also ask

How do you fix function is not defined?

To clear a text input, assign blank string to 'value' attribute (not innerHTML attribute) document . getElementById("text"). value = ""; And don't call myfunction in JS tab If you use a <form> then an input with 'type' attribute "reset" will do the same thing.

Why is JavaScript saying my function is not defined?

Make sure the function is defined inside your script If it's running without any error, then you may have several lines of code after the declaration that causes the script to malfunction.

Why is a function not defined?

A function is said to be "undefined" at points outside of its domain – for example, the real-valued function. is undefined for negative. (i.e., it assigns no value to negative arguments). In algebra, some arithmetic operations may not assign a meaning to certain values of its operands (e.g., division by zero).

Why function is not defined in jQuery?

You may experience the “jQuery is not defined error” when jQuery is included but not loaded. Make sure that it's loaded by finding the script source and pasting the URL in a new browser or tab. The snippet of text you should look for to find the URL to test.


1 Answers

If you are using GreaseMonkey, any functions you define are sandboxed by GM and not available in the main window.
When you use any of the native functions however, like setTimeout or alert, they are called in the context of the main window e.g; when you call setTimeout you are actually calling window.setTimeout()

Now the function you have defined, mark doesn't exist in the main window and what you are asking setTimeout to do is evaluate the string 'mark()'. When the timeout fires window.eval( 'mark()' ) is called and as discussed, window.mark is not defined. So you have a couple of options:

1) Define mark on the window object. GM allows you to do this through the unsafeWindow object like this:

unsafeWindow.mark = function(){}
setTimeout( 'mark()', 10 );        //this works but is ugly, it uses eval

2) Pass a reference to the local mark to setTimeout:

function mark(){}
setTimeout( mark, 10 );        //this works too but you can't send parameters

But what if you need to send parameters? If you have defined your function on the main window, the eval method will work (but it is ugly - don't do it)

unsafeWindow.mark2 = function( param ) {
    alert( param )
}
setTimeout( 'mark2( "hello" )', 10 ); //this alerts hello

But this method will work for functions with parameters whether you have defined them on the main window or just in GM The call is wrapped in an anonymous function and passed into setTimeout

setTimeout( function() {
    mark2( "hello" )
}, 10 );                              //this alerts hello
like image 107
meouw Avatar answered Sep 19 '22 12:09

meouw