I am trying to find some javascript that will keep an input focused when the webpage is not the active page.
For example, I have 2 firefox windows open, one is for reading and the other has some simple counters (press the one of the input buttons and it counts).
The annoying issue is having to click back and forth between the windows (you have to have the counter window active before you can click the button). What I am hoping will work is making the last button pressed开发者_如何学Go stay focused even when the page is not active.
This is basically what I have been trying with jquery:
<html>
<head>
<script src="http://jqueryui.com/jquery-1.4.4.js"></script>
<script type="text/javascript">
$("input.focus:last").focus();
</script>
</head>
<body>
<input type="button" name="counter1" size="10" class="focus" value="button1">
<input type="button" name="counter2" size="10" class="focus" value="button2">
</body>
</html>
Wrap your code in a DOMReady
handler:
$(function() {
$(window).bind('blur', function() {
$("input.focus:last").focus();
}).trigger('blur'); //run straight away
});
jsFiddle example
$(document).ready(function(){
var button = $("input.focus:last");
button.blur(function(){
try {$(this).focus();}
catch(Error){}
});
button.focus();
});
add try catch too. so when your input object is not visible, they dont get error
精彩评论