473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Noob Q: Different ways to run code in script tags

I found some code in a sample web page that ends like this:
.....

<script>
(function () {...good stuff here...})();
</script>
</body>
</html>

So it builds an anonymous function and immediately invokes it. Fine.

Now I see on another page:

<script>
....good stuff here...
</script>

So... what's up with that? I guess it is a two-part question:

1. What is the difference?

2. When is either preferred?

I can see maybe the anonymous function would have different scope, or
maybe avoid polluting the global namespace. I imagine it also runs
closer together in time because the whole thing gets parsed and then
dispatched all at once.

Am I getting warm?

kt
Nov 13 '08 #1
1 1893
Kenny wrote:
I can see maybe the anonymous function would have different scope, or
maybe avoid polluting the global namespace.
Avoiding polluting the global namespace is the main reason I think.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 13 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bouke Woudstra | last post by:
Hi, I'm a bit stuck with this python script. It's aim is to encode all flac files to wav and then to mp3. The only problem I have is to preserve the tags. The code works when there's just one...
4
by: Matt | last post by:
i know more-or-less zero about javascript, but my client wants a rollover and he created this code, which doesn't work. All comments are appreciated. 1. the tag: <td align="center" width="66"...
4
by: areese801 | last post by:
i am using a website with a form that passes criteria to a javascript to run a query rather than just appending my criteria to parts of the URL. i need to import a bunch of records into MS...
5
by: Christian Decker | last post by:
Hi, I'm trying to figure out how my users may use (or better misuse) the input fields of my site to to insert JavaScript. I'm already replacing all occurencies of "script" in every input field...
61
by: phil-news-nospam | last post by:
Why does SVG need a different tag than other images? IMHO, SVG should be implemented as an image type just like any other image type, allowing it to work with <img> tags, and ... here is the...
13
by: anil.rita | last post by:
When the user chooses an AV file to play, based upon the type of file, I want to use the default installed media player to play it. I am wondering if this is a good way - any alternatives,...
6
by: plemon | last post by:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
6
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.