0

I have create a HTML page with some JavaScript code. Here is the code

   <script type="text/javascript">
     function doIt(){
        window.location = document.getElementById('url').value;
    }
   </script>
   <input type="text" id="url" />

I want that when some one press the enter after type the URL. The page redirect to the given URL automatically. How i can do this? Please suggest me some code

1
  • are you familiar with jQuery? Commented Jan 5, 2012 at 18:06

6 Answers 6

1

Use below code:

<html>
<html>
<head>
<script type="text/javascript">
     function doIt(){     
       window.location.href = document.getElementById('url').value;
    }   
    document.onkeypress=function(e){
        var e=window.event || e;        
        if(e.keyCode===13) doIt();       
    }
</script>
</head>
<body>
<input type="text" id="url" value="http://www."/> 
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Your attaching the keypress to the document element. That is not the ideal solution...
@Xander, your code is correct. but,mine is very simple to understand. anyway, you are open to say that you are correct, when no one said you are wrong :)
1

see live demo at http://jsfiddle.net/qiao/RGqwD/2/

var input = document.getElementById('url');
input.onkeydown = function(e) {
  var key = e.keyCode || e.which;
    if (key == 13) {
      window.location = input.value;
    }
};

Comments

1

Here is solution: http://jsfiddle.net/YBLQx/1/

JS

(function () {

    var textField = document.getElementById('foo');

    function addHandler(el, event, handler) {
        if (el.addEventListener) {
            el.addEventListener (event, handler, false);
        } else if (el.attachEvent) {
            el.attachEvent (event, handler);
        }
    }

    addHandler(textField, 'keydown', textEntered);

    function textEntered(event) {
        if (event.keyCode === 13) {
            window.location = textField.value;
        }
    }
}());

HTML

<input type="text" id="foo" />

Best regards!

Comments

1
<input type="text" id="url" onblur="window.location.href='this.value'" />

the above is if the focus is taken out, like pressing tab

and then for enter, write onkeydown handler

<script type="text/javascript">
  document.getElementById('url').onkeydown = function(e) {
      var keyCode = e.keyCode || e.which;

     if (keyCode === 13) {
       window.location.href = this.value;
     }
 }


</script>

Comments

1

If your not using jQuery, the following is a complete example:

function addEvent(obj, type, fn) {
    if (obj.attachEvent) {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function() {
            obj['e' + type + fn](window.event);
        }
        obj.attachEvent('on' + type, obj[type + fn]);
    } else obj.addEventListener(type, fn, false);
}

addEvent(window, 'load', function() {
    addEvent(document.getElementById('url'), 'keyup', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);

        if (code == 13)
            window.location.href = this.value;
    });
});

Example - http://jsfiddle.net/4PDt4/

Credits for the addEvent code snippet: - http://ejohn.org/projects/flexible-javascript-events/

3 Comments

How i can use this function in my code? can please post an example?
@Xander should i use jquery file?
@RizwanKhan jQuery will simplify - I've updated my answer to provide a complete example...
0

You need to attach a keypress event handler to the text box. In your event handler, you would check if the key pressed was enter. If so, do the redirect. Look up event handlers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.