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

How to avoid the execution of a function at the time of defining.

Hi All,

How to avoid the execution of a function at the time of
defining.

Here i am giving the details.
I am creating the following div container through DOM.

<div id="content">
<a href="#" onclick="displayDiv('content')" Click</a>
</div>

The Code is

var category_list = document.getElementById('category_list');
var dom_div = document.createElement('div');
dom_div.id = 'content';
var dom_link = document.createElement('a');
dom_link.href ='#';
dom_link.onclick = displayDiv();
val = document.createTextNode('Click');
dom_link.appendChild(val);
dom_div.appendChild(dom_link);
category_list.appendChild(dom_div);
The displayDiv Funcion
function displayDiv()
{
dv = document.getElementById('content'); //Here is the error.
}

The Problem is when the following script
dom_link.onclick = displayDiv('content');
is executed it is calling the function displayDiv(name)
Here we have the code
document.getElementById('content');
which throws the error.

The reason is the div container is not yet created.

What I need is the function should be called only on the click event .
It should not be called while I define it to the Click Event. (ie it
should not be called at the time of defining)
How to achieve this.

Thanks in Advance
Regards
Moses

Mar 18 '07 #1
6 1433
On Mar 19, 6:07 am, "Moses" <mosesdinaka...@gmail.comwrote:
Hi All,

How to avoid the execution of a function at the time of
defining.

Here i am giving the details.

I am creating the following div container through DOM.

<div id="content">
<a href="#" onclick="displayDiv('content')" Click</a>
</div>

The Code is

var category_list = document.getElementById('category_list');
var dom_div = document.createElement('div');
dom_div.id = 'content';
var dom_link = document.createElement('a');
dom_link.href ='#';
dom_link.onclick = displayDiv();
That will execute the function immediately and assign the result, you
want to assign a reference so remove the call operator:

dom_link.onclick = displayDiv;
--
Rob

Mar 18 '07 #2
Hi Rob,

Thanks for your reply,

dom_link.onclick = displayDiv;

If I need to pass parameters what to do?
Regards
Moses

Mar 19 '07 #3
On Mar 18, 9:51 pm, "Moses" <mosesdinaka...@gmail.comwrote:
Hi Rob,

Thanks for your reply,

dom_link.onclick = displayDiv;

If I need to pass parameters what to do?
dom_link.onclick = function(){ displayDiv(para1,para2) }
or
dom_link.onclick = 'displayDiv(para1,para2)'

Mar 19 '07 #4

Thank You :)

Moses

Mar 19 '07 #5
On Mar 19, 1:51 pm, "Moses" <mosesdinaka...@gmail.comwrote:
Hi Rob,

Thanks for your reply,

dom_link.onclick = displayDiv;

If I need to pass parameters what to do?

dom_link.onclick = function() {
displayDiv(parm1, parm2);
}
However, if the parameters are local variables of the outer function,
you will be creating closures. That usually isn't an issue but may
have unexpected or unwanted effects (or not).

To avoid closures, use (wrapped for posting):

dom_link.onclick = new Function(
'displayDiv(' + parm1 + ', ' + parm2 + ')');
--
Rob

Mar 19 '07 #6
On Mar 19, 1:59 pm, scripts.cont...@gmail.com wrote:
On Mar 18, 9:51 pm, "Moses" <mosesdinaka...@gmail.comwrote:
Hi Rob,
Thanks for your reply,
dom_link.onclick = displayDiv;
If I need to pass parameters what to do?
[...]
dom_link.onclick = 'displayDiv(para1,para2)'
In what browsers do you expect that to work? Do you have an example
that works in a good percentage of browsers?
--
Rob

Mar 19 '07 #7

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

Similar topics

7
by: Damien | last post by:
Hello to all, I've written a script that take quite a long time to execute (email sending). Last time I ran it, PHP gave me a "execution time limit exceeded" (or something like that), so I changed...
12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
13
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something...
17
by: Pushkar Pradhan | last post by:
I want to time my matrix multiply code (in MFLOPS). I want to run the code 100,000 times or some other big number. This can be done 2 ways (for and while loops): timer1 = time(NULL); for(n = 0;...
6
by: Berimor | last post by:
Hi, i've been always used the in_array() function to check availabylity of a value in array. Until today. Simple code: (do not run it tho :) ) <? function getmicrotime(){ list($usec, $sec) =...
6
by: Steve Edwards | last post by:
Hi, I'm trying to find a simple way to identify which of a heirachy of classes a particular class is: class Agent { public: enum { class_ID = 'Agnt' }; Agent();
17
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
6
by: monkeyboy | last post by:
Hello, I have a function that hotshot says is very slow. I can get the aggregate execution time, but is there a way to get the execution time of each line so I can find the bottleneck? Thank...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.