Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent an iframe from stealing focus

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?

like image 610
jaysonp Avatar asked Aug 26 '11 16:08

jaysonp


2 Answers

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"

like image 90
Lobster Fighter Avatar answered Sep 20 '22 01:09

Lobster Fighter


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.

like image 35
Wladimir Palant Avatar answered Sep 19 '22 01:09

Wladimir Palant