Is it possible to fire events picked up by a separate browser window? With JavaScript perhaps.
i.e. if my website opened up another window to display summary product information, would it be possible to notify that window to update when a different product is selected in the main window?
Needs to work in IE, but doesn't need to work on Chrome or other browsers.
Use code like this:
Parent window
var func = function() {...}
child = window.open(...)
Child window
window.opener.func(); // Call function in parent window
You can also call function in the child window from the parent but there is one problem: You must wait until the child window has finished loading; window.open() is asynchronous.
More details: Accessing parent window from child window or vice versa using JavaScript
There are a couple of options.
Cross-Document-Messaging allows you to pass events between windows. For this to work, you need to have a handle of the target window, which you can really only acquire if the target is either an <iframe> (e.g. through window.frames, or window.parent from the iframe's POV) or if the target is a window opened by the current window through window.open() or window.opener from the popup's POV.
Shared Workers can connect otherwise unassociated windows much like Cross-Document-Messaging. Shared Workers are only available in Chrome and Safari, though.
Server-Sent Events could use the server to proxy the communication between your otherwise unassociated windows. This reqires a round-trip to the server (so is not entirely client-based) and is not available in Internet Explorer.
Web Sockets are an option, too. They too suffer from a server round trip and are not available in Internet Explorer. There is socket.io, which polyfills this functionality down to old Internet Explorers - so might be a viable solution.
A hacky solution is abusing LocalStorage. It'll work in modern Browsers and IE8.
jQuery BrowserEvent was something I played with way back when. It abuses the window.event and browsers' local storage capabilites to simulate passing events between browsers. This is nothing you'd want to use in a production environment, though.
German fellows may want to check out Kommunikations-APIs in HTML5 - Welche wann nutzen?
Yeah, that would be possible if the window with product information would be open using window.open method in parent window.
That way you can induce communication between those two (without, strictly speaking, events) but with some extra code that's possible.
Related
I have a problem. I need to return to parent Url from Frame opened in a Android Webview.
The sequence is: Open inside Webview new frame. Select in frame options and paramters. Call in frame javascript function like _"javascript:parent.opener.jsfunction"_. Parent Web doesn't open...
I don't have access to Web. I work only in Android side.
I test Web in a firefox for Android and it works.
Need help.
By default, WebView doesn't support multiple windows. If you check, I believe the parent field actually isn't set and doesn't point to the parent window (or anything at all). The same applies to other similar fields like opener and top.
You might be able to work through this by enabling support for multiple windows and then implementing onCreateWindow in your WebChromeClient. I think there's some more you have to do, but it's been a while and I don't recall the details.
One way I've hacked around this in the past is to use setJavascriptInterface and just set the name to parent or whichever field you want. Implement the appropriate methods as necessary on your Java object. This can get a bit messy, but it works.
Is there any JavaScript syntax to make sure, across prevalent browsers, merely single child window will be created when users click buttons or hyperlinks?
Thanks.
Just always use the same target whether it's a link or a window.open call.
foo
window.open("bar.html", "child");
In which context are you talking about. Checking whether a tab of a certain kind is already open is not possible because it imposes security risks.
However, if you are suggesting to only have one window opened as Invoked by:
window.open(strUrl, strWindowName, strWindowFeatures)
Then you will only be able to have one window open at any time with a specified strWindowName.
I'm trying to control one window from another in HTML5. I'd like it so when I open one window that I log into, and another window that I login to, then from one window I can click a button and something happens in the other window. I'm not sure where to start; can somebody point me in the right direction?
You don't need any HTML5-Features for that but you can easily do it with javascript:
myNewWindow = window.open() opens a new window and assigns a window-object of that new window to myNewWindow, so you can easily access the new window's DOM from the opening script, using the myNewWindow-variable.
It also works the other way: In a script in the new window, you can user window.opener to access the window-object and DOM of the opening window.
Just make sure, that the content of all your windows is loaded from same domain, as javascript does not allow you to control content loaded from another source (refer to "same origin policy" for more information on this topic).
This answer is 6 months late, but for the Googlers (like me):
I would recommend WebSockets for what you're trying to do Jonathan. WebSockets allow the server to broadcast data to any/all browser windows that are open for your page/site in realtime.
Browser support is currently quite poor but there are good shims to get it working across all browsers very efficiently. Socket.io is one such solution which I've used to great success.
I am very new to JavaScript. Kindly note that I am trying below issue in a shell which overrides many JavaScript functions.
I have an issue with focusing a window: on a single "click" action, I navigate to a new page which has two JavaScript methods which launch two external URLs which I don't own. For example I launch Yahoo.com and Google.com. My JS launches Yahoo.com in current window (as a page navigate) and Google.com as a pop-up. I WANT Google.com WINDOW TO BE FOCUSED irrespective of loading time of either URLs. The major issue is I cannot use the setTimeout JS function as this function's behavior is altered within the shell and is not usable.
Note: I am using a custom reusable JS function to launch external URLs and I just pass values to that method. So I don't even have access to window object. If I can somehow achieve a time delay without using setTimeout, it will be ideal case. If not, I will have to override that custom JS function, get access to the window object. Even if I have control over those window objects for external URLs, since loading times are different, setting focus to the Google window object is not always giving me the focus on Google window.
(IE6 & 7)
You cannot guarantee the behavior you want, in general; browsers will not let you.
Safari generally ignores requests to focus windows. Firefox and I think Chrome can be configured by their users (not by your code) to allow focus requests, but by default they won't.
When flash has keyboard focus, CTRL+T (new tab) and CTRL+N (new window) are intercepted by flash.
Is there a way to pass these events through to the browser so that they work (opening new tab, opening new browser) OR is there a javascript command for these actions?
This is a long standing issue with Flash and browsers. (And I mean long - check out this eight-year-old bug on Mozilla browsers.) The problem is that Flash intercepts all input events, rather than the browser. It's sandboxed in its own environment, and doesn't pass events back to the browser.
Conceptually, this isn't necessarily a bad thing. What happens when Flash wants to listen to a ctrl + n event? Should the browser take focus away from Flash because it uses that hotkey already? It'd be a real pain for Flash developers, that is for sure.
There have been proposals on how to fix this issue that I've seen for particular browsers, but there's no catch-all solution. For example, this solution is referenced in the bug, but it obviously won't work the way you want (since the user will have to jump through quite a few hoops to get it working).
So... no, for now. Would be really neat if this problem could be fixed.
Closest you could get is to have ActionScript trigger Javascript to open a blank window to a blank URL
// We abstract it in a function here in case we want to
// change it later
function openBlankWindow()
{
window.open( '' );
}
For most people, this will launch a new window or a new tab (depending on their browser preferences) but since it is being initiated by the web page, may be subject to pop-up blockers.
There is no way to actually ask the browser to specifically do one of the two tasks you are asking about. I would be a security/annoyance nightmare if web pages had the permissions/privileges to do that.