0

I'm trying to use the focus() function to focus on a parent window after clicking on a link that runs a function. The code I have works in IE, but it doesn't work in Chrome for some reason.

Here is the HTML with the function (moveToNext(var1,var2)):

<td style="border-left: solid 1px #000000;border-bottom: solid 1px #000000;" nowrap="">
    <a href="javascript:moveToNext('xxxxxxxxxxxx','null')">xxxxxxxxxxxx</a>
</td>

Here is the code in the function:

function moveToNext(var1,var2){
    ...
    // Other code to set form.action
    ...

    window.opener.document.forms[0].submit();
    window.opener.document.getElementById('elementid').focus();
    return;
}

I also saw this question, document.getElementById(id).focus() is not working for firefox or chrome, but this didn't work for me below:

window.opener.document.forms[0].submit();
window.setTimeout(function() {
    window.opener.document.getElementById('elementid').focus();
}, 0);

The function is working in Chrome because the form is being submitted, the focus() just isn't working.

Anybody know how to fix this or have any suggestions?

Nathaniel Brown
  • 61
  • 2
  • 4
  • 11
  • how does that code get triggered? clicking a button? – Robbie Milejczak Dec 04 '17 at 19:40
  • 1
    well isn't the form sumbission submitting a form so how would focusing work if the page is making the request? – epascarello Dec 04 '17 at 20:02
  • @RobbieMilejczak I have a link that calls the function to run the code. – Nathaniel Brown Dec 04 '17 at 20:18
  • @epascarello it's submitting the form to the parent window. The code works fine in IE, but just won't focus in Chrome. – Nathaniel Brown Dec 04 '17 at 20:20
  • ALl I am saying if the form is submitting the page and it is reloading, how are you supposed to set focus to something that is exiting and rerendering? – epascarello Dec 04 '17 at 20:21
  • @epascarello idk, maybe I'm just not knowledgeable enough on this. The focus is working in another browser though, could that be IE being faulty? Also, if it makes a difference, blur() doesn't work either when called on the child window that isn't reloading – Nathaniel Brown Dec 04 '17 at 20:38
  • @NathanielBrown so it's a click event? you might need to pass the event as an argument to the function you're calling on click and call `event.preventDefauklt()` – Robbie Milejczak Dec 04 '17 at 20:40
  • @RobbieMilejczak I added the html to the post. The function is in a link and serves to define the form's action and submit the form. It's then supposed to focus on the main window with the code I posted above without closing the child window. – Nathaniel Brown Dec 04 '17 at 21:11

0 Answers0