Stop focus to the embedded form

My client's site has an embedded Formstack form using iframe. The problem is during the page/iframe loading, the browser autofocus to the first field (i.e. First Name textbox). This results unwanted scrolling down. How to fix this?
7 Replies
Chris Bolson
Chris Bolson6d ago
You may have to use JS to find the element that is getting the autofocus and set it to blur(). However that might be too late to prevent the scroll 🤔
Daryl M
Daryl M6d ago
I ended up adding display: none; to that iframe. Then add display: block; after the page loads. You're right - it will be too late.
Chris Bolson
Chris Bolson6d ago
Following on from your idea, rather than updating the display, as this will no doubt cause the code to "jump", you could update the iframe src after the page loads, adding a placeholder url with no content in the meantime.
Jochem
Jochem6d ago
you might also be able to use iframeElement.contentWindow.scrollTo() to scroll it back to the top
MarkBoots
MarkBoots6d ago
I never used it before, but maybe one of the sandbox attribute values can help here? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#sandbox
MDN Web Docs
: The Inline Frame element - HTML: HyperText Markup Language | MDN
The HTML element represents a nested browsing context, embedding another HTML page into the current one.
Squeemeister
Squeemeister5d ago
I know this might seem like a "duhh" question but... Does Formstack give you the ability to disable autofocus on the first field? I've never used them so I've no clue, but that feels like a setting you should be able to access when managing or creating a form.
ἔρως
ἔρως5d ago
also, have you tried to lazy load the iframe with loading="lazy"?