I have a website that embeds an iframe. When I was testing the iframe, I used google.com and noticed the search input field took focus. Clearly, I won't be using google.com for production but would like to prevent the iframe from stealing focus.
Is there a way to prevent an iframe from stealing focus?
If you have access to server side scripting you can use it to download a live copy of the page you want to embed, search it and remove any focus stealing code, then display that modified page in your iframe. Or if you find there is no focus stealing code, you can just link your iframe to the remote page as usual.
Another option might be to initially hide the iframe with CSS style="display:none"
and allow the user to unhide it with javascript Object.style.display="inline"
Not really. You can put the focus back on your window if the focus moves away (WARNING: I don't recommend using that code):
<body onblur="window.focus();">
This has some not so nice side-effects like not being able to focus the location bar in Firefox or getting into endless loops if the frame also tries to fight for the focus. So if you want to do this (that's a big "if", I don't recommend it) you should at least limit it to the page loading phase and allow the focus to be changed after that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With