2

I am trying to open a new tab in Blazor Server. I am currently using JavaScript:

        [Inject]
        private IJSRuntime JSRuntime { get; set; } = null!;

        JSRuntime.InvokeVoidAsync("open", PaymentWindowUrl, "_blank");

This does attempt to open the Url in a new tab, however it is blocked by the browser's popup blocker and has to be allowed before it appears.

I would like to do this without the user having to click a button or link manually.

Seems to be OK in Edge but the popup issue occurs on Google Chrome.

Have tried to place an a href tag on the page and then do a .click() instead with JavaScript but the issue still occurs.

Is there anyway I can bypass this? I am unable to find much on the internet. Or do I have to bite the bullet and let the user allow the popup?

ZackB93
  • 21
  • 2
  • 1
    Popups can be and have been misused, that's why Chrome blocks them in some cases. I'm afraid there is nothing that can be done about this. I assume you had a look at https://stackoverflow.com/q/4602964/5470544? – JSON Derulo Apr 26 '23 at 10:42
  • 1
    The reason it gets blocked is because of a limitation from the browsers that popups can only be opened as a direct response to user input. The callstack cannot be traced back to the original event since it passes over the network before the function is invoked. This is only an issue on Blazor server, because for WASM it never leaves the browser. – Axekan Apr 26 '23 at 11:08
  • 1
    If you provided more context to what you were trying to accomplish, I could give a better answer. However, I got around this issue by setting up a plain JavaScript event listener instead. – Axekan Apr 26 '23 at 11:09
  • @Axekan I was just trying to open up another page in a new tab. The page contains a submit form that is connected to Barclay's payment system and does some more logic on our side. – ZackB93 Apr 26 '23 at 11:17
  • Thank you for your replies - I will have a look at an event listener instead - but I may just bite the bullet instead. – ZackB93 Apr 26 '23 at 11:18
  • I know this doesn't answer your question directly, but have you considered doing a redirect instead? The vast majority of shops I've interacted with do this instead of opening a popup. Popups can even be disabled in the browser. – Thomas Glaser Apr 27 '23 at 08:58

0 Answers0