5

I have created a pdf on the server when i use:

        function GetPdf(document) {


            //Stores the data and creates the html,pdf file
            $http.post('createpdf/', document).success(function(data){

                console.log(data.filename);

                window.open('download2/'+data.filename+".pdf", "_self");


            });

I get a error message pop up blocked in google chrome. When i use the option enable pop ups for this website it all works fine. Is there any way around this ? Because this could be confusing for some users.

But when i use:

window.open('download2/'+data.filename+".pdf", "_self");

It opens the page without warnings but then the main application is replaced by the pdf which is not the result i want to have.

Greg
  • 1,690
  • 4
  • 26
  • 52

4 Answers4

7

Browsers have strict rules about when they allow JavaScript to show a popup, but they can be summarized as "Only in response to a user action".

Receiving a response to an HTTP request is not a user action, so popups are banned.

The simple solution here is to not use JavaScript. The point of Ajax is to communicate with the server without leaving the page, but you're going to leave the page anyway so there isn't really any point in using Ajax.

Just use a regular form submission.

<form method="post" action="createpdf/" target="_blank">

… then have the server side script redirect to the URL of the created PDF instead of returning the URL as JSON.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I guess you are using and external JavaScript library, I had the same issue on another Project, I used target="_tab" and it worked, I found this on this question. It's the way Chrome handles popup calls from JavaScript when you use libraries, I used Moment.js to trigger a similar event and got the same issue.

Community
  • 1
  • 1
David Ortega
  • 915
  • 9
  • 25
-1

Pop up blocking is not an issue, but a native browser feature that protect the users from popup-hell. I would recommend to open the PDF in a modal popup instead of a new browser window.

With some jQuery code it is quite easy to implement: documentation is found here

Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
-1

You can always use an alternative route, for example instead of window.open function. You can use the window.location function, perhaps. Windows.location.replace which will relocate you in the same tab.

<script type="text/javascript">
<!--loc can be any changed to your window-->
var loc = "https://google.com/";
window.
window.onclick = function() {
   window.open(loc);
}
</script>

Try that :) window.open is being blocked because you are doing window.open without a click function. Most web browsers will block this feature for security purposes.

aidangig
  • 199
  • 1
  • 12
  • I update my question as you can i can't use window.onclick – Greg Dec 04 '15 at 15:28
  • Window.open with a onclick function is the best waty to go. Are you just trying to open a new tab on chrome? Can you be more specific? – aidangig Dec 04 '15 at 15:32