diff --git a/layout/base/nsPresShell.cpp b/layout/base/nsPresShell.cpp index 4dab20d6d242..235bff86b553 100644 --- a/layout/base/nsPresShell.cpp +++ b/layout/base/nsPresShell.cpp @@ -4323,8 +4323,20 @@ PresShell::GoToAnchor(const nsAString& aAnchorName, PRBool aScroll) sel->AddRange(range); } } - // Selection is at anchor, but put focus on the document - esm->ChangeFocusWith(nsnull, nsIEventStateManager::eEventFocusedByApplication); + // Selection is at anchor. + // Now focus the document itself if focus is on an element within it. + nsCOMPtr win = + do_QueryInterface(mDocument->GetScriptGlobalObject()); + if (win) { + nsCOMPtr focusController = win->GetRootFocusController(); + if (focusController) { + nsCOMPtr focusedWin; + focusController->GetFocusedWindow(getter_AddRefs(focusedWin)); + if (SameCOMIdentity(win, focusedWin)) { + esm->ChangeFocusWith(nsnull, nsIEventStateManager::eEventFocusedByApplication); + } + } + } } } else { rv = NS_ERROR_FAILURE; //changed to NS_OK in quirks mode if ScrollTo is called