473,386 Members | 1,798 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,386 software developers and data experts.

If else for literal

Can some sort of is else condition be used for the following to change the
onclick to onmouseover depending on an argument in the surrounding function?
All of the code in the .. code to execute ... is very long and exactly
the same for both onclick or onmouseover events. Otherwise I would just make
2 functions.
function doIt(argument){
element.onclick = function {
.. code to execute ...
};
}
-------------------------------------
Laymens terms, like this..

function doIt(argument){
if(argument == 1){
element.onclick = function {
}else{
element.onmouseover = function {
}
.. code to execute ...
};
}
David

Feb 19 '07 #1
7 1495
On Feb 19, 12:17 pm, "David" <n...@none.netwrote:
Can some sort of is else condition be used for the following to change the
onclick to onmouseover depending on an argument in the surrounding function?
All of the code in the .. code to execute ... is very long and exactly
the same for both onclick or onmouseover events. Otherwise I would just make
2 functions.

function doIt(argument){
element.onclick = function {
.. code to execute ...
};}

-------------------------------------
Laymens terms, like this..

function doIt(argument){
if(argument == 1){
element.onclick = function {}else{

element.onmouseover = function {}

.. code to execute ...
};

}

David
Ahh, carrying bad PHP habits into the browser space...

For one thing, you shouldn't assign rogue function handlers in
function bodies, mostly because IE's garbage collector can't clean
them up:

http://blogs.msdn.com/ie/archive/200...iciencies.aspx

but also because a new copy of the anonymous function is created for
each element, which is much less efficient than having all elements
sharing the same function:

var userAction = function(e) {
// your code here
}

function doIt(argument) {
if (argument == 1) {
element.onclick = userAction;
} else {
element.onmouseover = userAction;
}
}

-David

Feb 19 '07 #2
David wrote:
Can some sort of is else condition be used for the following
to change the onclick to onmouseover depending on an argument
in the surrounding function? All of the code in the .. code
to execute ... is very long and exactly the same for both
onclick or onmouseover events. Otherwise I would just make 2 functions.
function doIt(argument){
element.onclick = function {
.. code to execute ...
};
}
-------------------------------------
Laymens terms, like this..

function doIt(argument){
if(argument == 1){
element.onclick = function {
}else{
element.onmouseover = function {
}
.. code to execute ...
};
}
You could do:-

element[((argument == 1)?'onclick':'onmouseover')] = function(){ ...

See:-
<URL: http://jibbering.com/faq/faq_notes/square_brackets.html >

- but that is going to be pretty obscure source code, and a slightly
unexpected design to be suing the same function for one of either onclick
or onmouseover depending on a parameter.

Richard.

Feb 19 '07 #3
[..]
"David Golightly" <da******@gmail.comwrote in message
Ahh, carrying bad PHP habits into the browser space...

For one thing, you shouldn't assign rogue function handlers in
function bodies, mostly because IE's garbage collector can't clean
them up:
but also because a new copy of the anonymous function is created for
each element, which is much less efficient than having all elements
sharing the same function:
[..]

var userAction = function(e) {
// your code here
}

function doIt(argument) {
if (argument == 1) {
element.onclick = userAction;
} else {
element.onmouseover = userAction;
}
}

Thanks for the info. I may have to rethink the script because ( using your
example ) the argument for the "element" is inside of the userAction var.
The doIt() function has no knowledge of what the "element" is.

David
Feb 19 '07 #4

"Richard Cornford" wrote:
You could do:-

element[((argument == 1)?'onclick':'onmouseover')] = function(){ ...

See:-
<URL: http://jibbering.com/faq/faq_notes/square_brackets.html >

- but that is going to be pretty obscure source code, and a slightly
unexpected design to be suing the same function for one of either onclick
or onmouseover depending on a parameter.

Richard.
That works. I had no idea you could put if else statements inside of the
brackets like that.

Thnkas, David
Feb 19 '07 #5

"David Golightly" <da******@gmail.comwrote >
>
For one thing, you shouldn't assign rogue function handlers in
function bodies, mostly because IE's garbage collector can't clean
them up:

http://blogs.msdn.com/ie/archive/200...iciencies.aspx

but also because a new copy of the anonymous function is created for
each element, which is much less efficient than having all elements
sharing the same function:

You know, I don't think this would apply in this case. The outer function is
called only once onload to initiate the script, and the inner function. So
this function and the nested function objects ( applied to specific id'd
anchor tags ) are already stored in memory. I don't think extra instances of
these function objetcs are created when the event happens?

David
Feb 20 '07 #6
On Feb 20, 6:53 am, "David" <n...@none.netwrote:
"David Golightly" <davig...@gmail.comwrote >
For one thing, you shouldn't assign rogue function handlers in
function bodies, mostly because IE's garbage collector can't clean
them up:
http://blogs.msdn.com/ie/archive/200...performance-re...
but also because a new copy of the anonymous function is created for
each element, which is much less efficient than having all elements
sharing the same function:

You know, I don't think this would apply in this case. The outer function is
called only once onload to initiate the script, and the inner function. So
this function and the nested function objects ( applied to specific id'd
anchor tags ) are already stored in memory. I don't think extra instances of
these function objetcs are created when the event happens?

David
It's true that, if you only call this once, you'll only get one copy
of the function. But you're still getting into a potentially
problematic circular reference with your closure. Any time a function
exists has an element in its scope that it in turn is attached to (as
an event handler), you're creating exactly the kind of circular
reference referred to in the MSIE blog article - the one the IE team
decided wasn't worth fixing.

Also, were you to put this event handler assignment (doIt) in a loop,
you'd get multiple copies of the function literal assigned as
properties of your different DOM objects, instead of your DOM objects
having references to the same function object, since with each outer
function call you'll get a different scope chain and therefore a new
closure. See:

http://developer.mozilla.org/en/docs...considerations

Finally, it's probably not good programming practice to just say "Oh,
I'll just do it like this because it's a one-off thing, no need to
worry about scalability." If I can get the same result using a more
scalable technique, I get in the habit of doing it that way. You
never know when you may need to scale in the future! Just my 2c,
however; you're welcome to develop your own technique.

-David

Feb 20 '07 #7

"David Golightly" wrote
you're creating exactly the kind of circular
reference referred to in the MSIE blog article - the one the IE team
decided wasn't worth fixing.
lol...
Finally, it's probably not good programming practice to just say "Oh,
I'll just do it like this because it's a one-off thing, no need to
worry about scalability." If I can get the same result using a more
scalable technique, I get in the habit of doing it that way. You
never know when you may need to scale in the future! Just my 2c,
however; you're welcome to develop your own technique.
This function will only be called once onload and that's it. It will never
be placed in a looping condition ( scary thought ), however I do agree with
you about good coding habits. I'm not a rebel trying to re-create new
techniques nor take the easy way out.

Thanks again,
David


Feb 21 '07 #8

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

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
6
by: Joe | last post by:
I know that the Literal control will not render a <span> tag so I can not format its text. Other than this, what is the difference betwen the Literal control and the LiteralControl Control? How...
4
by: Neo Geshel | last post by:
Just moved to C# from VB.NET, frustrated to hell and back by inability to get much-copied (from about 20+ different resources) literal example to work. Master Page content: <meta...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
2
by: =?Utf-8?B?U2NvdHQgTA==?= | last post by:
I have a button and a literal on my page (among other things but *I think* this functionality is pretty much isolated). 1. In Page_Init I set literal.Text = "PAGEINIT" 2. In the button's click...
0
by: Hetal | last post by:
Hi, I have been working on creating a dynamic table with controls on a ASP.NET webpage and i have been using literal controls to do that. The issue that i am facing is, when i have the Start and...
1
by: Hetal | last post by:
Hi, I have been working on creating a dynamic table with controls on a ASP.NET webpage and i have been using literal controls to do that. The issue that i am facing is, when i have the Start and...
3
by: Tex08 | last post by:
I have hit a roadblock on a class project. Trying to implement the composite pattern with separate classes to represent a boolean expression. My literal class will not compile (g++, required...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.