Self invoking functions javascript
Asked Answered
S

7

4

I wrote a self invoking function in both firefox and chrome it it wouldn't invoke.

I wrote something to the effect of

(function () { alert("THE"); })();

do self invoking functions not work in current browsers?

I did include all essential tags and all other code works on the page

Springing answered 16/7, 2011 at 6:33 Comment(3)
"self invoking"? what do you mean? btw, it does work on chromeCarboloy
Do you have a link to the page? That code works. What if you replace it with alert("THE"); without the function. Does the alert execute? I don't think so.Monometallic
this part of the code works perfecly. Show any surrounding code. Make sure there is no other JavaScript that produces an error (you can check Firebug's or Chrome's console)Rugging
D
11

"Self-invoking functions" are not really a part of javascript, it's just a term that people are calling a specific pattern of code (like AJAX, etc.); these patterns should work anywhere that javascript works.

What you're calling a "self-invoking function" is just creating an anonymous function and immediately calling it (as opposed to say storing it in a var, as an object value, as a function param, etc.).

That is, the following are basically the same:

var f = function(){...}; f()

and

( function(){...} )()

So because your 'self-invoking function' is a basic part of javascript, there is no possible way it's not working unless the insides aren't working or your environment is messed up. You could copy-paste your code onto a new blank page, and it would work fine. Something else must be going wrong:

Check your errors in your dev console. Specifically, check to make sure you don't have a syntax error or that there isn't some weird thing going on with the webpage you're testing it on (e.g. if you somehow redefine alert...).

Devest answered 16/7, 2011 at 6:43 Comment(1)
You're right the problem was I had a syntax error, the statement before it had an implicit semi-colon, which should have been explicit. It caused a parsing errorSpringing
W
4

I had this issue with a self invoking function which produced this error...

Uncaught TypeError: object is not a function

The problem was caused by not having a semi colon ending the line before the opening bracket

Wormy answered 22/2, 2012 at 15:24 Comment(1)
Yeah I had the same error, added the semicolon and voila.Recuperate
A
3
<script type="text/javascript">
  (function() {
     alert('Hello World!');
  })();
</script>

Works in every browser I have installed on this machine.

Asmara answered 16/7, 2011 at 6:37 Comment(0)
M
2

That function works. Javascript supports functional programming, so for a browser not to run that code, even for a very old browser that would be absurd. Are you sure that statement is being reached? Try debugging javascript that occurs before that statement.

Monometallic answered 16/7, 2011 at 6:36 Comment(0)
R
0

This function definitely works. I would check your browser's console for any js errors in your page. Perhaps you could try to put a simple console.log function at the beginning of your script to see if any JavaScript is being called in the first place.

Rizas answered 15/12, 2013 at 1:38 Comment(0)
S
0

This self invoking function with return value will work in all current browsers(Safari, Chrome and Firefox) without issue. This function executes immediately, automatically and anonymously.

<script type="text/javascript">
    alert((function(){
        return("Hello World");
    })());
</script>
Sanhedrin answered 29/10, 2014 at 0:32 Comment(0)
P
-1

I had a similar problem. I'm mentioning it below.

I couldn't run this self-invoking function on any browser

(function myfunc() {
    var x = 34;
    console.log(x);
})();

but whenever I added window.onload like below, it worked fine:

window.onload = (function myfunc() {
    var x = 34;
    console.log(x);
})();
Prewar answered 5/10, 2022 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.