Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Problem in triggering a function

    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
    Last edited by otnj2ee; 10-07-2008 at 03:00 AM.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    function createButton(actionClick){
    var newButton = document.createElement('a');
    newButton.onclick = function() {
      setTimeout(actionClick, 0);
    };
    //append the element
    ...
    
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •