English 中文(简体)
Self invoking functions javascript
原标题:

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

最佳回答

"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...).

问题回答

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

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.

<script type="text/javascript">
  (function() {
     alert( Hello World! );
  })();
</script>

Works in every browser I have installed on this machine.

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.

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>

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);
})();




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签