0

Actually my real scenario is

When user opens the modal pop up (say bootstrap modal pop up http://getbootstrap.com/javascript/#modals), we need to open a url which is already saved by the user.

So for that I have tried to open with window.open. But this is always blocking me. Is there any way to overcome this?

The following scenarios I have tried.

When I try to open using window.open(link, '_blank'); and if browser has the blocked the pop up, then I restricted by browser to open the url in new tab.

To over come this I have tried like (following) creating anchor link and triggering click event. But this is also not also working.

var link = document.createElement('a');
link.setAttribute('href', url);
link.target="_blank";
var event = document.createEvent('MouseEvents');
                                            event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
                                            link.dispatchEvent(event);

Some websites by passing this feature.

For example, https://www.online.citibank.co.in/.

When I click log in button, a new window is opening. They also doing the same functionality as window.open. But the browser is not blocking citibank new window.

I don't know what is the reason behind this. How to implement this feature?

Jeeva J
  • 3,173
  • 10
  • 38
  • 85
  • You've tagged jQuery in your OP, but your code is Vanilla JS. Are you looking for solutions using either way? Also *when* do you try and open the login page? Is on a page load event? Or when the user clicks a link etc.? – Geoff James Aug 31 '16 at 10:06
  • Even if you create an anchor link in JS/jQuery, and invoke the `click()` programmatically (I've just tried) - it seems that the browser still sees it as a popup (as it's done from code, and not a "real" click). What's stopping you from just having a "Login" button on the page? I'd imagine that's what the example site you've given is using? – Geoff James Aug 31 '16 at 10:22
  • I am trying to open another website url from my sites using window.open – Jeeva J Aug 31 '16 at 10:34
  • I can see that from your OP. What isn't clear is *when*, and why you can't just use a normal `` in your page? Could could answer the questions I've asked, and someone might be able to help you better? – Geoff James Aug 31 '16 at 10:35
  • The reason I ask is "*Popup blockers will typically only allow window.open if used during the processing of a user event (like a click)*" source: http://stackoverflow.com/a/9514875/6240567 – Geoff James Aug 31 '16 at 10:40
  • This is not about login page. In my side, There won't be any button on the ui. When opening the dialog box, based on the user saved value, we will redirect the links which we saved earlier by user. So there won't be any button for the user to click and navigate. We need to navigate to the url based on user saved values. – Jeeva J Aug 31 '16 at 10:49
  • OK, well as per my previous comment it looks like you can't stop `window.open()` being blocked from non-user-actions. "*When opening the dialog box...*": What dialog box? You haven't mentioned one? Can you include more detail/explanation in your OP, as it is currently very unclear what exactly you're asking and what exact desired outcome you're after. – Geoff James Aug 31 '16 at 10:52
  • @GeoffJames, please update your comment as answer.. – Jeeva J Aug 31 '16 at 12:14
  • Which one? Apart from the question "why doesn't my `window.open()` work.."; which would be a duplicate of another question - It's still unclear what *exactly* you're doing/asking/expect. How can I answer a question to which I do not know what the question is? – Geoff James Aug 31 '16 at 12:28
  • The following is the answer for my question.I tried on button click using window.open after ur comment. This has been worked. But in my scenario, I never show a button to the user to be clicked. In other questions, I didn't find this comments as solution. "Even if you create an anchor link in JS/jQuery, and invoke the click() programmatically (I've just tried) - it seems that the browser still sees it as a popup (as it's done from code, and not a "real" click). What's stopping you from just having a "Login" button on the page? I'd imagine that's what the example site you've given is using?" – Jeeva J Aug 31 '16 at 15:30
  • 1
    Pop up blockers block pop ups not initiated by a user action. That is how they work. If there was a work around than pop up blockers are really useless. :) – epascarello Aug 31 '16 at 15:44
  • I know, @epascarello - that's what I've been trying to say to OP, and get an idea *exactly* what UX they want, but to no avail *sigh* – Geoff James Aug 31 '16 at 16:09
  • Nice quote @epascarello. Great. – Jeeva J Sep 01 '16 at 09:09

1 Answers1

3

It's not clear where/when you're triggering trying to open the link, and judging by your need for using JS, I guess it's not through click of an existing button/link.

The issue I see you having is this:

Even if you create an anchor link in JS/jQuery, and invoke the click() programmatically (I've just tried) - it seems that the browser/popup-blocker still sees it as a popup (as it's done from code, and not a "real" click).

Doing some digging, it seems there isn't anyway to get round this. I found this quote on another answer on SO:

"Popup blockers will typically only allow window.open if used during the processing of a user event (like a click)" source: https://stackoverflow.com/a/9514875/6240567


Anyway, you can use jQuery to create an element and click() it, in one line. My browser still detects it as a "popup", so use it with this caveat in mind

You can do it like so:

  • Create an a element with jQuery
  • Simulate clicking the href, using the .click() function (preceded by [0] indexer - TL;DR)

jQuery:

$('<a href="http://your.link.com" target="_blank">&nbsp;</a>')[0].click();

or Vanilla JS:

window.open("yourlink.com", "_target");

Just put this inside the handler/method for where/when you'd like the "link" to be clicked.

Hope this helps! :)

Community
  • 1
  • 1
Geoff James
  • 3,122
  • 1
  • 17
  • 36