When you load a page from another domain into an iframe , you cannot do anything on an iframe page from JavaScript on your page.
As for the browser, the iframe is a separate window and you do not have access to it. Imagine that a user had a bank page open in one window, and your page was open in another window. You know that the browser will not allow your JavaScript code to interfere with the bank page, right?
The same applies when another page is in an iframe . This is still a completely separate browser window, it is simply positioned so that it looks like its page.
In your working example, the code works because the username and password fields are not in the iframe from another domain. They are part of the same page as the JavaScript code.
If the iframe loading from the same domain as your main page, you can do whatever you want, including filling out the form fields. The code will be slightly different because you need to access the iframe document instead of the main page document, but it is easy. But if the iframe belongs to another domain, you're out of luck.
source share