473,726 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Opera function visibility oddity

Dear Javascript Experts,

Opera seems to have different ideas about the visibility of Javascript
functions than other browsers. For example, if I have this code:

if (1==2) {
function invisible() {
alert("invisibl e() called");
}
}

then most browsers will consider invisible() to be undefined. But in
Opera you can call it! You can try this online here:
http://chezphil.org/bugreps/funcdecl.html - with most browsers you'll
get an error (open your javascript console) but Opera will show the
alert.

So: is this an Opera bug, or is either behaviour acceptable? (I'd be
interested to know what Safari and any other less-common browsers do.)

--Phil.

Jul 23 '05 #1
5 2843
I wouldn't call this a bug-- it's why we can define functions at the
bottom of our script, or in the middle, or all over the place, and use
them anywhere. It's expected that function definitions using the
function statement will work regardless of where you put them. I've
actually had the same experience with IE (Win32).

If you really don't want that function defined except in certain
circumstances, try something like this:

if( foo )
bar = function ( arg1, arg2 ) {
// statements
}

Alternatively, you may use the Function constructor, but that can be
somewhat ponderous.
phil_g...@treef ic.com wrote:
Dear Javascript Experts,

Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code:

if (1==2) {
function invisible() {
alert("invisibl e() called");
}
}

then most browsers will consider invisible() to be undefined. But in
Opera you can call it! You can try this online here:
http://chezphil.org/bugreps/funcdecl.html - with most browsers you'll
get an error (open your javascript console) but Opera will show the
alert.

So: is this an Opera bug, or is either behaviour acceptable? (I'd be
interested to know what Safari and any other less-common browsers do.)
--Phil.


Jul 23 '05 #2
On 24/05/2005 12:22, ph*******@treef ic.com wrote:

[snip]
if (1==2) {
function invisible() {
alert("invisibl e() called");
}
}


This is a syntax error. I imagine it is not flagged because it is
considered too common, and will break too many scripts. However, it is a
mistake and should never occur within a script.
There are three ways to create a function object in ECMAScript: the
function declaration, the function expression, and the Function
constructor function.

The first, the function declaration, is fairly simple and recognisable:

function <identifier> ( <argument list> ) {
<function body>
}

What is not always appreciated is that function declarations can only
appear in two general locations: at the program (aka root, or global)
level, and within the function body of another function. So,

/* Program */
function myFunction() {
}

and

/* Program */
function myOuterFunction () {

/* Function body */
function myInnerFunction () {
}

}

are fine. However,

if(condition) {

/* Statement list */
function myFunction() {
}

}

is not because statements like if and while expect other statements.

Function declarations are somewhat special. Unlike most source elements,
they are evaluated before execution would normally reach them. This is
why you can call a function before you have defined it.

When a script is initially parsed, all function declarations at the
program level will be evaluated, and the corresponding function objects
will be created. It is only after this process that execution begins.
Similarly, when a function is called, any inner function declarations
(such as myInnerFunction , above) are evaluated before execution of that
function commences.
The second, the function expression, is a little awkward to explain,
mainly because it might be difficult to see the distinction with
function declarations. Essentially, a function expression works like any
other expression: it is not evaluated until execution reaches it. This
allows you to create functions conditionally:

var func;

if(condition) {

func = function( <argument list> ) {
<function body>
};

}

Only if condition is true will the expression be evaluated. If condition
is false, func will remain undefined.

So, how /do/ you distinguish between a function expression and a
declarations? Essentially, it depends on the beginning of a source
element. If it starts with a 'function' token, it is a declaration. If
it starts with something else, it's a function expression. For example,

(function() {
/* ... */
})();

is a function expression because the source element begins with an
opening parenthesis.

A function expression can still have an identifier

func = function <identifier>( <argument list> ) {
<function body>
};

but this is only so the function can refer to itself - for recursion,
for example. It's not to be used outside of the function body.
I'll ignore the Function constructor for now. I'm sure the above is more
than enough information. What I will say is that although the Function
constructor can be used to create function objects at runtime like
function expressions, there are major differences between the two.

[snip]

If something needs more explanation, please ask.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
> Opera seems to have different ideas about the visibility of Javascript
functions than other browsers. For example, if I have this code:

if (1==2) {
function invisible() {
alert("invisibl e() called");
}
}

then most browsers will consider invisible() to be undefined. But in
Opera you can call it!


This behaviour is by design.

If you want function definition to depend on an if clause, do the following

if (1==2) {
var invisible = function () {
alert("invisibl e() called");
}
}
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #4
I wrote:
if (1==2) {
function invisible() {
alert("invisibl e() called");
}
}

Mike Winter replied: This is a syntax error
[snip lots of useful explanation]


Thanks Mike. I had just started to look it up in the Ecma
specification and was coming to the same conclusion.

I had come across this in the following context:

if (detect_some_br owser) {
... declare functions for that browser ...
} else if (detect_some_ot her_browser) {
... declare function for that other browser ...
}

and it took a long time to understand why Opera was executing the
special IE versions of the functions, rather than the "all other
browser" ones.

Ideally, of course, all browsers would reject this with an error
message. I encourage Opera to either do what the other browsers do
(the pragmatic approach) or to reject it with an error message (the
purist approach). What they currently do, i.e. accept the invalid code
but interpret it differently from the other browsers, is confusing.

--Phil.

Jul 23 '05 #5
On 24/05/2005 14:36, ph*******@treef ic.com wrote:

[snip]
if (detect_some_br owser) {


Browser detection is flawed. Instead, you should be examining the
capabilities of any particular host. For example, Opera supports quite
of few of Microsoft's proprietary capabilities, as well as those defined
by the W3C.

See the browser detection entry in the group FAQ, and the related
article in the FAQ notes.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6

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

Similar topics

10
3599
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script language="JavaScript" type="text/javascript"> function show(that) { if (box.style.visibility=='hidden') { that.style.visibility = 'visible'}; }
3
1667
by: Michael Phillips | last post by:
Hello I am trying to use DOM to create an object in a document and wish to be compativle with Opera My code is containerDiv = document.createElement("div"); //create download container div containerDiv.setAttribute("id", "statusContainer");
60
4312
by: A.Translator | last post by:
I have been struggling with a page that is based on a table structure (because that is all I am feeling comfortable with at the moment) styled with css. One of the problems I run in to is Opera (Opera 7 in my case). The weird thing is: viewing the page through Opera's eyes the first time around is OK, but after refreshing, the page gets messed up badly. I will look into the cause of the mess, but for now am really curious: what does...
17
1663
by: windandwaves | last post by:
Hi Folk I recently completed an interactive map. I have now discovered that it is not working in Opera. The address is: http://switch.hosts.net.nz/~admin64/search.php The map is basically 20+ images over each other, each of which are transparent gifs.
2
1559
by: torbs | last post by:
Hi I have two quicktime players. I load them like this: <object id="'movie" class="synligActiveXSpille" (...) <embed name="movie class="SynligSpiller (...) References to the two players are kept in the properties of the object ..loadingPlayer and .activePlayer.
2
2781
by: zeljko.markic | last post by:
Strangely I came acros a problem in IE performing little asp/javascript model: code: <script type="text/javascript"> function turn_on_files(folder) { x=event.clientX+25 y=event.clientY+25 document.getElementById('files_frame').style="visibility:visible;z-index=9;position:absolute;left:"+x+"px;top:"+y+"px;";
25
4563
Atli
by: Atli | last post by:
Hi. I've created this little window that is shown when I roll the mouse over elements I specify in my html code. Like a nubbin I test this in IE while I'me writing it, thinking it can hardly be that hard to modify for the other browsers when I'm done. Now it works perfectly in IE but no matter what I do I can not make it work for Firefox or Opera. Please, can anybody tell me what those !#"#$%%) browsers arn't understanding? :P
8
1619
by: pitstop10 | last post by:
I use cutehtml as these are my starting days with Javascripts. The script generated by CuteHTML works in IE and Opera. It does not however work in Mozilla. its a slideshow of images. The problem with Mozilla is that the page loads and the slideshow never loads giving me a static blank area where the slideshow should have loaded. I am pasting the script below. <script language="JavaScript"> <!--
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.