Is using window.opener to call a javascript function good practice?
I would never have considered using window.opener to call a javascript function in the parent window but hav开发者_JAVA百科e come accross it recently and would like to know.
- Is it considered good practice to do so? I would have thought it is something that you should avoid and with ajax something that you could always design around.
- Is it mainly left over from the days before ajax and tabed browsing?
Just to be absolutely clear, window.opener
is a reference to the window that opened the current window using window.open()
. It is the only way to get hold of such a window in JavaScript. You have to be careful when talking about "parent" windows, since that could mean the window containing the current frame or iframe, and window.parent
is a reference to the window object containing the current window.
As to your questions:
Thankfully, it's a lot less common than it used to be to open new windows. It's always a bad idea to do it without the user's knowledge and consent, or without the user explicitly taking an action such as clicking on something to trigger it. Most people now have pop-up blockers, which will often suppress your new window. In short, there may be a few use cases where it's a reasonable thing to do (I like to have a separate JavaScript logging window when developing, for example) but in general it's best avoided.
I don't think Ajax or tabbed browsing were big influences on the decline of the practice of opening new windows. I would say the biggest single influence was an increased appreciation of the confusion and annoyance it causes users, led by prominent usability experts like Jakob Nielsen (see http://diveintoaccessibility.org/day_16_not_opening_new_windows.html and http://www.useit.com/alertbox/990530.html). Also, many ads used pop-ups, which was extremely annoying for users and led directly to the rise of pop-up blockers.
精彩评论