Input type text and onKeyDown not working under IE

I am writing a WWW application, it has to run under IE. I have the problem with the code that runs under Firefox, but I can’t get it running under IE.

// JS code

function test()
{
    if (window.event.keyCode == 13)
        window.location.assign("myPage.php");
}

I’ve tried some similar ways around window.location and location.href, also document.location. I’ve read that IE has problems with that, so I ask for a solution.

The goal is, that page reloads after typing in some text into <input type='text' name='item_code' onKeyDown='test()'> and click enter. So the result is similar to pressing submit type button below the text input.

Within IE it reloads the same page and nothing happens. In Firefox it works correctly.

UPDATE 1:

Tried solution given by bobince.

<input type='text' name='item_code'>

<script type='text/javascript' >
 
document.getElementsByName('item_code')[0].onkeydown = function(event)
{
    if (event == undefined) { event = window.event; }
    if (event.keyCode == 13) { window.location = 'myPage.php'; }
        
    alert('1');
}
    
</script>";

The problem is, that if there is alert('1'); line, page shows alert and redirects, if there isn’t alert('1'); line, page just reloads to itself. I don’t know what is the problem here?

UPDATE 2:

I’am pasting what finally works for me.

<form action='mainPage.php' method='POST'>
    <input type='text' name='item_code'>
</form>

<script type='text/javascript' >
    document.getElementsByName('item_code')[0].onkeydown= function(event)
    {
        if (event == undefined)
        {    
            event = window.event;
        }

        if (event.keyCode == 13)
        {
            var js_item_code = document.getElementsByName('item_code')[0].value;
            window.location = 'myPage.php?item_code='+js_item_code;
            return false;
        }
    };
</script>

68 thoughts on “Input type text and onKeyDown not working under IE”

Leave a Comment