PDA

View Full Version : JavaScript Error - Unexpected call to method or property access



srinivasb
06-28-2012, 07:39 AM
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.

vwphillips
06-28-2012, 10:35 AM
if I understand the requirement this works for me


<!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>

srinivasb
06-28-2012, 11:37 AM
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();