Results 1 to 3 of 3

Thread: JavaScript Error - Unexpected call to method or property access

  1. #1
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JavaScript Error - Unexpected call to method or property access

    In my case if we click on link it will open one floater. In that floter if we click on one of the link("close"), the above error i am getting.
    My code:
    var globalLastFocus = {
    elem: null,
    getState: function (opt) {
    if (document.activeElement) {globalLastFocus.elem = opt || document.activeElement;}
    },
    setState: function () {
    if (globalLastFocus.elem !== null) {
    globalLastFocus.elem.focus();//this is causing the above error
    globalLastFocus.elem = null;
    }
    }
    };


    I am getting the above error at "globalLastFocus.elem.focus();" line in the following code.

    I am unable identify the root cause for this issue. Please suggest me how to resolve this issue.

    Thanks in advance.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    if I understand the requirement this works for me

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    var globalLastFocus = {
     elem: null,
     getState: function (opt) {
    document.Show.Show0.value=vic++;
                 if (document.activeElement) {
                  this.elem = opt || document.activeElement;
                 }
                },
    setState: function () {
    document.Show.Show1.value=this.elem;
               if (this.elem) {
                this.elem.focus();//this is causing the above error
                this.elem = null;
               }
              }
    };
    
    /*]]>*/
    </script></head>
    
    <body>
    <input type="button" name="" value="TEST" onfocus="globalLastFocus.setState();"/>
    <div onmouseup="globalLastFocus.getState();" >TEST</div>
    <script> vic=0; </script>
    <form name=Show id=Show style="position:absolute;visibility:visible;top:700px;left:0px;" >
    <input size=100 name=Show0 >
    <input size=10 name=Show1 onfocus="globalLastFocus.getState();" >
    <input size=10 name=Show2 >
    <input size=10 name=Show3 >
    <input size=10 name=Show4 >
    <input size=10 name=Show5 >
    <input size=10 name=Show6 >
    <input size=10 name=Show7 >
    <input size=10 name=Show8 >
    <input size=10 name=Show9 ><br>
    <textarea name=TA rows=1 cols=100 ></textarea>
    </form>
    
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    srinivasb (06-28-2012)

  4. #3
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I have got the solution for this issue. We need to use setActive() for IE and for remaining browsers we need to use the focus() method.


    if IE browser :
    ===========
    document.activeElement.setActive();


    for remaining browsers:
    =================
    document.activeElement.focus();

Bookmarks

Posting Permissions

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