简体   繁体   English

使用Javascript集中新页面中的现有HTML页面

[英]Focus existing HTML page from new page using Javascript

I have a simple site with links to different pages in the top bar. 我有一个简单的网站,顶部栏中有指向不同页面的链接。

Something like this 像这样

==============================================
 Index link  |  Page 1  |  Page 2  |  Page 3
==============================================

The 'site' is a user interface for a scientific data system on a ship and users will often navigate between the different links depending on the task they wish to perform. “站点”是船上科学数据系统的用户界面,用户通常会根据他们希望执行的任务在不同的链接之间导航。 The idea is to limit the number of open pages to the links in the top bar, in other words, not having more than one 'Page 1' or 'Page 2' open within one browser session, but rather just navigate to the existing page. 想法是将打开页面的数量限制为顶部栏中的链接,也就是说,在一个浏览器会话中打开的“页面1”或“页面2”不超过一个,而只是导航到现有页面。 These pages contain dynamic JQueryUI information and this will be lost if a new page is opened. 这些页面包含动态JQueryUI信息,如果打开新页面,这些信息将丢失。

I need to be able to give focus to existing pages or open them if they are not already visible. 我需要能够将重点放在现有页面上,或者如果它们不可见则将其打开。

The code below works well if the calling page opened the page needing focus: 如果调用页面打开了需要聚焦的页面,则下面的代码可以很好地工作:

function launchApplication(l_url, l_windowName)
{
  if ( typeof launchApplication.winRefs == 'undefined' )
  {
     launchApplication.winRefs = {};
  }
  if ( typeof launchApplication.winRefs[l_windowName] == 'undefined' || launchApplication.winRefs[l_windowName].closed )
  {
    launchApplication.winRefs[l_windowName] = window.open(l_url, l_windowName);
  } else {
    launchApplication.winRefs[l_windowName].focus()
  }
}

My problem is referencing pages that were not spawned by the calling page. 我的问题是引用不是由调用页面生成的页面。

I have done quite a bit of research and it seems that due to security considerations, open browser windows not spawned by the calling page are not accessible. 我已经进行了大量研究,并且由于安全考虑,似乎无法访问未由调用页面生成的打开的浏览器窗口。

Is there any way I can implement this functionality? 有什么办法可以实现此功能? I know the URLs and window names for all pages and browsers are limited to Chrome and Firefox. 我知道所有页面和浏览器的URL和窗口名称仅限于Chrome和Firefox。

Any advice greatly appreciated. 任何建议,不胜感激。

Caveat: Trying to manipulate windows on browsers is really difficult and hard to get (and keep) working cross-browser, because of all the nefarious things that have been done with it in the past. 注意事项:尝试使用浏览器操作窗口确实非常困难,并且很难(并保持)跨浏览器工作,这是因为过去使用它进行过的所有恶意操作。 I recommend putting all of the content in a single window and doing the tabs within that window, via jQuery UI tabs or similar. 我建议将所有内容放在单个窗口中,并通过jQuery UI选项卡或类似方法在该窗口中执行选项 This gives you full, unambiguous control over what's showing when. 这使您可以对何时显示的内容进行完全,明确的控制。

But answer the actual question: 但是回答实际问题:

If you know the window names, then you can get access to those windows even if the page didn't open them (provided there are no cross-origin issues). 如果您知道窗口名称,那么即使页面未打开它们也可以访问这些窗口(前提是没有跨域问题)。

var wnd = window.open("", "windowname");

...will give you a reference to the existing window with that name if there is one, without opening a new window. ...将在没有打开新窗口的情况下为您提供一个具有该名称的现有窗口的引用。 If there isn't a window with that name, it will open a new, blank window with that name. 如果没有一个具有该名称的窗口,它将打开一个具有该名称的新空白窗口。

So I'd probably use that, and then look in wnd.document to see if the content is there. 所以我可能会用它,然后在wnd.document中查看内容是否存在。 If it isn't, presumably you've just opened the window, so set wnd.location to the correct URL, like this: 如果不是,大概是您刚刚打开了窗口,因此将wnd.location设置为正确的URL,如下所示:

// Try to get existing window
var wnd = window.open("", "thewindowname");

// Does it have our content?
if (!wnd.document.getElementById("someElementId")) {
    // No, set the location
    wnd.location = "/some/url";
}

// Bring it to the foreground
wnd.focus();

Live Example | 现场示例 | Source - Note the counter running in the example, so you know whether you've just activated the same window without replacing its content (when it's already open). -请注意示例中运行的计数器,因此您知道是否刚刚激活了同一窗口而不替换其内容(当窗口已经打开时)。

The above works with windows opened with window.open (even if by another page) if the window name is given when opening, and with windows opened via links with target="thewindowname" . 如果打开时指定了窗口名称,则上述方法适用于使用window.open打开的窗口(即使通过另一页打开),以及通过target="thewindowname"链接打开的窗口。

This works for me in Chrome 23, IE8, and IE7 (and so probably IE6); 这在Chrome 23,IE8和IE7(可能还有IE6)中对我有效; it doesn't work for me in Firefox 17, Opera 11, or IE9 (and so probably not IE10). 它不适用于Firefox 17,Opera 11或IE9(因此可能不适用于IE10)。 Hence my earlier caveat. 因此,我之前的警告。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM