0

I'm having the following scenario in React:

  • I click on a button
  • I make an ajax call, I get an url as response
  • I want to open that url in a new tab without another click event

Is there a way I can do this in a single click event?

I tried the following code but no success:

return request(options, dispatch)
    .then(response => response.data.url ?
        (
            window.open(response.data.url, '_blank') 
        ) : (
            dispatch(updatedPage(id, response.data))
        )
    );

Cheers!

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
dtmiRRor
  • 581
  • 4
  • 17
  • not react related, to open a link in new tab the action has to be in click handler to prevent pop-up. consider prefetching whatever data you need. – Moti Korets May 08 '18 at 16:04
  • Please create a jsfiddle example of the code https://jsfiddle.net/boilerplate/react-jsx/ – Rohith Murali May 08 '18 at 16:17
  • thanks for your answer. I cannot prefetch any data because the ajax call sends quite a lot of data and i need that data to generate that link. – dtmiRRor May 08 '18 at 16:20
  • Whats the content of response.data.url? What's is the behavior you get when you try this? – Thiago Loddi May 08 '18 at 17:25
  • response.data.url is a string of this format: 'http://example.com/myurl' – dtmiRRor May 08 '18 at 20:42
  • have you tried adding the protocol to the URI? If I try window.open('google.com', '_blank') in my browser console it opens the same page I'm in. If run window.open('http://google.com', '_blank') it works as expected – Thiago Loddi May 09 '18 at 19:05
  • looks like chrome blocked the popup (an icon will be shown in the address bar on the right), so u need to manually accept the popups on the server. seems to work. thanks guys. – dtmiRRor May 10 '18 at 20:35

0 Answers0