window.open and .Net WebServices - javascript

I don't know exactly the cause of this symptom but here goes. In our web app, we call window.open to open a new window/tab (internally everyone's is set to open in a new tab). After opening this new tab, IE8 immediately switches the focus to it.
We then integrated a web service, so we call this web service which in turn invokes a callback javascript function when it returns. We are now calling window.open from inside the callback function. When we do this, IE8 opens the new tab but does not switch the focus. Calls to window.focus on the parent page and the child page do nothing.
Now, I know javascript is not threaded, but is this something to do with asynchronicity? Is there a way to get the browser to behave as expected?

I believe that by default, IE will only switch focus to the new tab if it was opened as a result of a user-initiated action such as a click. If the window.open call is not in response to a UI action then IE is probably opening the tab in the background as if it were some kind of popup, though obviously it's not being blocked by the popup blocker.
If you are in an intranet environment and can specify settings, have you tried setting the "Always switch to new tabs when they are created" check box on the tabbed browsing settings dialog?

Related

Why do Android browsers (Chrome/stock) force twitter intents to close then open in the same window, even when _blank or window.open are used?

Example of what I mean:
http://jsfiddle.net/dtipson/ttebddd5/2/
In all other browsers, and in cases not linking to twitter's intents pages, when you open a new window or target blank, it opens in a new tab. But with android, even once you've set the browser to handle links to twitter.com, the new window opens, then immediately closes, and then the original (calling) page navigates to twitter.com. Example code that won't work properly (though I doubt it's anything to do with this):
window.open(
'https://twitter.com/intent/tweet?text=hi',
'intent',
'scrollbars=yes,resizable=yes,toolbar=no,location=yes,status=no,width=550,height=420');
My guess is that this has something to do with have Android handles "application intents": if a page redirects to something that claims to have a native application link, it looks back up the chain of window.opener and affects the original page instead.
To try and block this behavior, I've tried using window.open to open a page that waits a few seconds and THEN redirects to twitter.com/intents. But even here, the new tab opens, waits for however many seconds on that transition page, and then right when it redirects, it closes itself and the original tab redirects to twitter.com instead. I've tried setting window.opener to null (even though that shouldn't do anything). I'm not sure how any code on twitter.com could even affect the original page as they are obviously not on the same domain (and I've tried setting things up so that the original domain does NOT have twitter's widgets.js on it, so they can't be using POSTMessage).
This really seems to be a (imho, bad) )quirk with how Android handles intents. Anyone know of any workarounds?

Close popups created with window.open in IE11

I have Ie 11 to support. I open the popup using var myWindow = window.open('url','name','width=640,height=480,menubar=no,toolbar=no');
however, the handle returned (myWindow) is always null in ie.
I need to close that popup after certain events happen.
I know that it is possible because I've seen other sites do that in the same browser.
Any ideas?
If window.open() is returning null, then something is wrong. You're likely blocking popups, which means that the Window object will not be created (and thus myWindow will be null).
Check your security settings there and enable them.
Once you get window.open() to execute successfully, you can call myWindow.close() to close the popup when necessary. Note that the close() method can only close popups that have been opened using the window.open() method.

AngularJS: How to open a file in a new tab?

LIVE DEMO
Given a URI of a file, I'd like to open it in a new tab (not a new window).
It looks like it is not possible to use $window.open(uri, '_blank').
So, I tried the following trick:
var link = angular.element('');
angular.element(document.body).append(link);
link[0].click();
link.remove();
and it works.
But, if I put exactly the same code in a promise callback, it doesn't work anymore (it opens the file in a new window instead).
Any idea what's going on here?
PLAYGROUND HERE
From your code/content, you can't force the browser to open a new tab (rather than a new window, or vice-versa). It's up to the browser settings to force it one way or another.
Anything else would be a security risk.
Let us understand fundamental how pop up blocker work.
If user trigger the function to open a new url, then pop up blocker will allow it(it should applied to any modern browser - at least firefox, chrome)
If not from user (like javascript function in background, promise or any other function trigger not from user), browser will block unless user whitelist the site manually.
This is not working.
function openInNewTab() {
window.open('http://stackoverflow.com','_blank');
}
openInNewTab();//fail
This is working
<h1><button onclick="openInNewTab()">Open In New Tab - working</button></h1>
I created simple plunkr version - http://plnkr.co/edit/QqsEzMtG5oawZsQq0XBV?p=preview
So, to answer your question. It is impossible unless user authorize it (user trigger it or white listed the site).
Quote from firefox -
Pop-up windows, or pop-ups, are windows that appear automatically
without your permission.
https://support.mozilla.org/en-US/kb/pop-blocker-settings-exceptions-troubleshooting
*Open in new tab / new windows not make any difference. Pop up blocker will still always block. It doesn't means that browser will allow if open in new tab. It is just coincidentally for certain browser default the settings in that manner.
Workaround
You can ask user explicitly to trigger the function to open in new tab after the background execution.
You can display message in UI to ask user to open the url.
Example - http://plnkr.co/edit/iyNzpg64DtsrijAGbHlT?p=preview
You can only open new windows inside click event handlers fired by the user.
The reason for this is usability.
I'm not sure if all browsers have this behavior but some browsers do not allow scripts to open windows without the user being noticed. Imagine when you visit a web page and suddenly, the web page opens several windows => it's annoying.
See this DEMO (tested with my Chrome and Firefox), even we trigger click event by script, the browser still blocks the popup.
$("#test").click(function(){
openInNewTab();
});
$("#test").click();
You cannot open a new window inside your ajax success callback because your ajax success is run in another cycle after the click event handler has finished its execution.
See this link for a workaround
if I put exactly the same code in a promise callback, it doesn't work
anymore (it opens the file in a new window instead).
I'm surprised that you're still able to open a new window. But this problem really has a lot of things to do with click events fired by the user.
Your problem is two-fold, and both folds tread on uncertain territory.
In the old days of browsers, window.open did exactly that – open a new window. That's because the concept of tabs hadn't been invented yet. When tabs were introduced, they were treated exactly like windows to improve compatibility, and that tradition continues to this day. That, and the fact that window.open was only standardized very recently, means that JavaScript cannot distinguish between windows and tabs.
There is no "normal" way to specify whether a link should open in a new tab or not. You can use the following hack, though: specify a custom window size to the open call (via the third argument), like so:
window.open('http://example.com', '', 'width=' + screen.width);
This will cause almost all browsers to open a separate window because tabs cannot have custom sizes.
In JavaScript, there are trusted events and untrusted events. Trusted events are, for example, legitimate clicks on a link by the user, whereas an untrusted event would be a manual click() call on a link.
Only trusted event handlers may open new windows/tabs. This is to prevent client-side attacks that crash the browser or confuse a user by rapidly opening a hundred tabs on mouseover or something similar.
Your second example doesn't work because the popup blocker blocks the untrusted event that you triggered via the click(). Although it was caused by a real click, the asynchronous call in-between severs the link to trustedness.
working version
$http.get('https://api.github.com/users/angular').then(openInNewTab());
EDIT----------------
Do not know why but a click() method called from a callback function acts differently than calling it straight.
You can see it here with a set interval example.
That is why I had call the function directly rather than going through a callback.
see it with timer callback
or you can use $window service please see here : http://plnkr.co/edit/8egebfFj4T3LwM0Kd64s?p=preview
angular.module("Demo", []).controller("DemoCtrl", function($scope, $http, $window) {
$scope.uri = 'http://martinfowler.com/ieeeSoftware/whenType.pdf';
function openInNewTab() {
var link = angular.element('');
angular.element(document.body).append(link);
link[0].click();
link.remove();
}
$scope.works = openInNewTab;
$scope.doesntWork = function() {
$http.get('https://api.github.com/users/angular').then($window.open($scope.uri));
};
});
For us the following worked well: http://blog-it.hypoport.de/2014/08/19/how-to-open-async-calls-in-a-new-tab-instead-of-new-window-within-an-angularjs-app/
In short: We remember the reference to the new window and changing the location afterwards.

Why does browser block popup windows in callback but not otherwise

Strange behavior - Im trying to open a new window in a callback - using Angular but probably a general JS issue.
If I do this:
$window.open('http://google.com', '_blank');
It works fine. However this doesn't as it gets blocked by my browser - I am using Safari 7 and have "block popup windows" checked:
Items.list(function(items) {
$window.open('http://google.com', '_blank')
});
Why does the browser block that and not the other and how can I circumvent this? I played with setTimeout as well as some SO post suggestion to assign $window.open to a variable before calling async but did not work here.
Popup blocker logic often blocks popup windows when the popup window is not opened as a direct consequence of a user action (like a click).
A callback that happens asynchronously is NOT a direct consequence of a user action - it's sometime later and is not directly connected to that action (as the browser sees it), thus the browser may not allow it.
The usual work-around is to open the popup window immediately (as a direct consequence of the user action) and then populate its content later after the asynchronous callback occurs and the content is available.
Does angular 6 support popup window like javascript? I am not looking for a modal window solution. I want to be able move the window to another screen like you can do with JavaScript popup windows. Is there any documentation on that? How can you go about doing it on Angular 5/6 ie to open a pop up window

Strange Window popup behaviour on IE7

We have written a small javascript function which checks if a URL should open in same window or a popup. In cases when a url should open in a new window IE is giving some strange behaviour a window popup flashes and closes with a beep sound. Can anybody suggest whats going behind the scenes i do not think its my javascript which is wrong. Is it some browser weird behaviour.
I suspect you have a third party pop-up blocker installed.
Yep, this sounds like some third party stuff. By default IE will try to guess what it should do with popups (open in a popup window, open in a new tab, don't open at all) but it won't try to open a window and close it immediately (unless caused by some addons or code included in the page). JavaScript errors shouldn't cause the window to close either (unless they really call window.close()).

Categories

Resources