Skip to content

Commit 195690d

Browse files
committed
Merge pull request defunkt#300 from defunkt/refocus
Improve focus emulation
2 parents 3fef6e7 + ef764ec commit 195690d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

jquery.pjax.js

+14
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,22 @@ function pjax(options) {
261261
window.history.replaceState(pjax.state, container.title, container.url)
262262
}
263263

264+
// Clear out any focused controls before inserting new page contents.
265+
document.activeElement.blur()
266+
264267
if (container.title) document.title = container.title
265268
context.html(container.contents)
269+
270+
// FF bug: Won't autofocus fields that are inserted via JS.
271+
// This behavior is incorrect. So if theres no current focus, autofocus
272+
// the last field.
273+
//
274+
// http://www.w3.org/html/wg/drafts/html/master/forms.html
275+
var autofocusEl = context.find('input[autofocus], textarea[autofocus]').last()[0]
276+
if (autofocusEl && document.activeElement !== autofocusEl) {
277+
autofocusEl.focus();
278+
}
279+
266280
executeScriptTags(container.scripts)
267281

268282
// Scroll to top by default

0 commit comments

Comments
 (0)