Re: javascript from form opening asset in pop up

JD_Nelson
Level 10 - Community Advisor

javascript from form opening asset in pop up

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?

8 REPLIES 8
Kenny_Elkington
Marketo Employee

Re: javascript from form opening asset in pop up

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>

JD_Nelson
Level 10 - Community Advisor

Re: javascript from form opening asset in pop up

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

Kenny_Elkington
Marketo Employee

Re: javascript from form opening asset in pop up

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 - Community Moderator

Re: javascript from form opening asset in pop up

Well actually the url in the onSuccess arguments will shadow the url from the closure...

SanfordWhiteman
Level 10 - Community Moderator

Re: javascript from form opening asset in pop up

You can set your PDF URL as the Follow Up URL (in Form Editor).

SanfordWhiteman
Level 10 - Community Moderator

Re: javascript from form opening asset in pop up

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 - Champion Alumni

Re: javascript from form opening asset in pop up

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 - Community Moderator

Re: javascript from form opening asset in pop up

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.