1

I have in my application an autocomplete input field. When jsp with this autocomplete input field opens I get error 'names' is undefined in line <body onload="getNames(names)">. This happens only in IE8,7,9. In Chrome and Firefox everything is fine. In controller I fetch user's names and surnames and write them in one string seperated with $ and then I send that string allUsersRoleUserAC to jsp. Method getNames(names) splits that string and put them to var tags.

Here is jsp code:

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Admin page</title>

<link rel="stylesheet" type="text/css" href="<%=response.encodeURL(request.getContextPath() + "/css/jquery-ui.css") %>" />
<link rel="Stylesheet" type="text/css" href="<%=response.encodeURL(request.getContextPath() + "/css/DOMAlert.css") %>" media="screen" />
<script type="text/javascript" src="<%=response.encodeURL(request.getContextPath() + "/JavaScript/DOMAlert.js") %>"></script>
<script type="text/javascript" src="<%=response.encodeURL(request.getContextPath() + "/JavaScript/jquery-1.8.2.js") %>"></script>
<script type="text/javascript" src="<%=response.encodeURL(request.getContextPath() + "/JavaScript/jquery-ui.js") %>"></script>
<script type="text/javascript">

var tags = new Array();
function getNames(names){
    tags = names.value.split('$');
}

</script>

</head>
<body onload="getNames(names)" >

    <form action="<%=response.encodeURL(request.getContextPath() + "/admin/deleteUser.html") %>" method="post">
        <input id="names" type="hidden" value="${ requestScope['allUsersRoleUserAC'] }" />
        <input name="korisnik" class="userSelectDeleteUser" value="ime prezime" onclick="this.value=''" id="autocomplete" />
        <input class="izbrisiKorisnikaButtonPosition" id="buttonRightPart" type="button" value="Izbriši" />
    </form>

    <script type="text/javascript">
        $( "#autocomplete" ).autocomplete({
            source: function( request, response ) {
                var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
                response( $.grep( tags, function( item ){
                    return matcher.test( item );
                }) );
            }
        });
    </script>

</body>
</html>
Juraj Vlahović
  • 400
  • 2
  • 4
  • 12
  • 1
    Nothing to do with Java/JSP. Figure how to do it using JavaScript/HTML & that logic should be easy to use in the JSP. – Andrew Thompson Jun 10 '13 at 13:54
  • 1
    What do you expect `names` to be? It's not defined anywhere. – Bergi Jun 10 '13 at 13:56
  • Not sure that I understand you. I use javascript to this and it works fine in firefox but in IE it doesn't work. Do you know if I can do something to correct this to work in IE or some workaround? – Juraj Vlahović Jun 10 '13 at 13:58
  • Actually this was originally an IE-only thing. Can't believe it doesn't work any more… [IE/Chrome: are DOM tree elements global variables here?](http://stackoverflow.com/questions/3434278/ie-chrome-are-dom-tree-elements-global-variables-here) – Bergi Jun 10 '13 at 13:59
  • names is input field with id="names" – Juraj Vlahović Jun 10 '13 at 13:59

3 Answers3

4
onload="getNames(names)"

What is the world is names? You need to use getElementById to reference an element. You should not reference an element by an id directly.

onload="getNames(document.getElementById('names'));"
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

names is in fact undefined. Other browsers will just be passing the undefined value, whereas IE won't let you do that.

If you want to get the names element then you can use

document.getElementById('names')

I would advise putting that inside the onLoad function rather than passing it as a parameter though, to separate the logic out from the markup.

Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
1

onload="getNames($("#names").val())"

or non jQuery

onload="getNames(document.getElementById('names').value)"

rorypicko
  • 4,194
  • 3
  • 26
  • 43