javascript from form opening asset in pop up | Community
Skip to main content
Community Advisor
July 14, 2015
Question

javascript from form opening asset in pop up

  • July 14, 2015
  • 2 replies
  • 3252 views

I embedded a form into a non-marketo landing page but when the PDF opens it does so in a pop up window, which is being blocked by pop up blockers instead of downloading it. I asked my developer about doing it "onclick" instead but he wasn't sure if that would work without filling out the form or not...

form.onSuccess(function(values, followUpUrl){

   

    //download the PDF

    var a = document.createElement('a');

    a.href='***.pdf';

    a.target = '_blank';

    document.body.appendChild(a);

    a.click();

is there a different way to code this part?

2 replies

Kenny_Elkington
Adobe Employee
Adobe Employee
July 14, 2015

Hi JD,

The onSubmit event is part of the onclick submission, so you need to generate the window during the onSubmit event and then set the URL during onSuccess:

<script>
MktoForms2.whenReady(function (form){

  var newWin;
  form.onSubmit(function (){
    newWin = window.open('about:blank', 'myWindow');
  });
  form.onSuccess(function (values, url){
    newWin.location.replace(url);
    return false;
  });
});
</script>

JDNelson1Community AdvisorAuthor
Community Advisor
July 14, 2015

sounds great -- but stupid question - where do I put my pdf url?

Kenny_Elkington
Adobe Employee
Adobe Employee
July 14, 2015

You just need to set the value of the URL variable to your desired location:

<script>

var url = "www.example.com";//set your URL here
MktoForms2.whenReady(function (form){

  var newWin;
  form.onSubmit(function (){
    newWin = window.open('about:blank', 'myWindow');
  });
  form.onSuccess(function (values, url){
    newWin.location.replace(url);
    return false;
  });
});
</script>

SanfordWhiteman
Level 10
July 14, 2015

JD, just documenting Kenny's solution a little more for posterity, the reason it works is that browsers are rightly suspicious of a click event that isn't really in response to user action, such as the synthetic click() event in your original code.  You won't get satisfaction by fake-firing events this way.

Since the Forms 2.0 onSubmit handler is run in response to the user-triggered (i.e. real) click event, you can do activities in there that would otherwise be suspicious, like opening a popup.  Then later you can replace the document in the popup window (the replacement is not considered suspicious, though that might seem like an arbitrary decision by browser vendors!).

However, these days you can't really force PDF "downloading" if you don't have control of the server that hosts the asset.  FF, Chrome, and Safari have their own PDF viewers and they really, really want to use them.

Josh_Hill13
Level 10
July 15, 2015

Agree. Generally you do not want a pop up. We had this once and it was very confusing and gave us all sorts of setup trouble and user problems.

SanfordWhiteman
Level 10
July 15, 2015

Was trying to stay neutral on the UX question but I do agree with Josh.

The popup (or pop-behind) isn't the worst thing by any means and most power users will grok what happened.  But even if it "works" on a technical level for every lead, there'll still be an element of surprise for some people.  And least astonishment​ would tell us that you want to reduce the surprise to zero if possible.  Even if it takes an extra click, people recognize the sequence Click to submit ---> Click to download/save and power users will know how to right-click if saving is what they want.  Of course what should also be possible is changing the response headers for Marketo assets based on query strings, like in this idea.