How to use Javascript to restrict browsers to create singular child window - javascript

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.

Related

Cross browser-window events?

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.

Is there a way to find if browser pop-up window opened with window.open had the parameter 'scrollbars=yes'?

Is there a way to find if browser pop-up window opened with window.open had the parameter 'scrollbars=yes'?
I need to run specific code depending on scrollbars being on or off, meaning they might not be showing but they are still on.
Even if there is not general browser solution, any help for IE will be great.
Paul
I would try finding the code that handles the window.open and saving a global variable. Then you can access the global variable by using window.parent
Try:
window.scrollbars.visible
This should return true or false.
See https://developer.mozilla.org/en/DOM/window.scrollbars

How to open a new window (or in a tab), but not give it focus

I have a function that I want to open up a URL in a new tab on a click event, but not give that tab focus. Is this possible with javascript?
You can't steal focus from a newly opened window. It's a security feature preventing sites from "taking control" of your browser. That would be a browser configuration setting.
As far as "hiding" focus from a popup, you might be thinking of what's called "PopUnder". Basically you use window.open() and set the option _blank and the paramater alwaysLowered, but it will not work gracefully for an average website. It requires you to have a signed script and take advantage of the Netscape Security PrivilegeManager, like this:
netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesRead UniversalBrowserAccess"); // etc...
Sorry bro :)
You can attempt to open a new window, then set focus back to the current window. However, user settings may make this impossible, or it might already be the default behaviour.
Pop–unders are used by some web sites probably to disassociate the web site from the window (i.e. so you don't know where it came frome). So they are assuming a certain naivety on behalf of the user. They are considered spam and treated with the same contempt.
If you outline what it is you are trying to achieve using a pop–under, you might get advice on better ways of doing it. Or not. :-)
To my knowledge it is impossible bro.Since the user's browser Settings will conflict with your logic

Window focus for a faster loading pop-up

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.

Ways to detect CTRL-N or when a user opens a new window

How can we detect when a user opens a new window. The user is already authenticated and we make heavy use of sessions.
We were trying to avoid Ctrl+N javascript hooks but maybe that is an option.
I am assuming the request is the exact same URL...with Ctrl+N?
We were trying to avoid ctrl-n javascript hooks
Forget it. Whilst you could in theory try to catch keypress events for ‘n’ with the Control key modifier, there are any number of other ways to open a new window or tab which may be more likely to be used, and you won't be able to catch. File->New Window/Tab, middle click or shift-click link, middle click back/forward buttons, right-click-open-in-new-window, open bookmark in new tab, double-click browser icon...
The user is already authenticated and we make heavy use of sessions.
That shouldn't be a problem in itself. I guess what you mean is that your application is dumping all sorts of page-specific data in the session that it shouldn't have, and now you find the application breaks when you have more than one window open on it? Well, commiserations and happy rewriting.
In the meantime about all you can do is tell the user “please don't try to open two browser windows on the same application”. There are potential ways you can make JavaScript on one page notice that JavaScript is running on another page in the same domain at the same time, generally involving using document.cookie as a inter-page communications conduit. But that's also a bit fragile.
If opening a new window causes a problem in your application, then you should fix the application code to handle it instead of trying to apply an inconsistent and unreliable client-side "bandage". That's my opinion.
Why?
And anyway you can't detect it. User can open new window not only with Ctrl+N but also with File->New Window.
You could possibly put a window count into the session and increment it on window.onload and decrement it on window.onunload.
Imagine me tutting, sucking air through my teeth and going "better you than me, guvna" if you use that, though.
What I have done to solve this issue is when the user authenticates set the window name on valid login.
<script>
window.name = 'oneWindow';
</script>
And then on the master page do a javascript check:
<script>
if (window.history.length == 0 || window.name != 'oneWindow')
//history length to see if it's a new tab or opened in a new window 0 for IE, 1 for FF
//window name to see if it's a CTRL + N new window
</script>
If the check is true then hide/remove the main content of the page and show a message stating they are doing something unsupported.
This works when your login page is not tied into the master page.
If you do not have a master page then I would suggest putting the check on all your pages.
Yes and no,
You'll always see it if a control has focus, else the event is sent directly to the browser and the code on the page never hear about it.
In my experience you can't hijack the browser's shortcut, your mileage may vary. You are likely to know it happened but the browser will do its thing (for obvious reason)
In most browsers, the effect of Ctrl-N is to open a new window at the same URL as the old one and associate it with the same sessionID.
Your best bet would be to modify the back end code if possible and allow for such things. Breaking the browser's feature is never a good thing.

Categories

Resources