...

View Full Version : Problem in triggering a function



otnj2ee
10-07-2008, 01:54 AM
All the following is tested on IE (it seems working fine on Firefox)
It is weird that for a JSP, let's say I dynamically create a tag:

function createButton(actionClick){
var newButton = document.createElement('a');
newButton.href = 'javascript:' + actionClick;
//append the element
...

}

At the run time actionClick = "Obj.test()"
It will display like: OK(under it, is the anchor <a href="...">Text</a>).

Now if I lay it out like:

<script>

createButton("Obj.test()");
//Function placed after
Obj.test = function(){
alert("This is working");
}

</script>

What happended:The function Obj.test = function(){
will never get executed when the anchor is pressed.

In some situation, if I lay it out like:

<script>
//Load this function first
Obj.test = function(){
alert("This is working");
}


createButton("Obj.test()");

}

</script>
What happended:The function Obj.test = function(){
will get executed when the anchor is pressed.
But not for ALL the JSP, for in some cases it does not work.


However, if I place the function inside a file (test.js) and load the file in the head, like:

<head>
<script language="JavaScript" type="text/javascript" src="test/test.js?"></script>
</head>

The function Obj.test = function(){
will ALWAYS get executed when the anchor is pressed.


Why so?


Thanks


Scott

shyam
10-07-2008, 05:03 PM
function createButton(actionClick){
var newButton = document.createElement('a');
newButton.onclick = function() {
setTimeout(actionClick, 0);
};
//append the element
...

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum