1

I have been tasked to add a script file to a page when you click a button. Thought this would be easy. The script displays a popup a user can fill in a form.

I have my button click working and it adds the script file to the page and calls the script file I want calling.

 function loadPopUp() {
        var script = document.createElement('script');
        script.src = "/js/patpopup.js";
        var body = document.getElementsByTagName("body")[0];
        body.appendChild(script);
        console.log("clicked");
        return false;
    }

In the script I was given it is just a url //r1.dotmailer-surveys.com/scripts/popover/111111?a=uc that was wrapped in script tags. I can't get the popup to open it only takes me to the page where all the script is.

If I wrap it in script tags and place it in the page it opens ok.

What should I put in the patpopup.js file that the script url is in, I have tried using window.location and various other methods to try and get the page to open in the page and produce the popup but always opens the script instead of running it. At the minute I only have the url //r1.dotmailer-surveys.com/scripts/popover/111111?a=uc in my patpopup.js file

StudentRik
  • 1,049
  • 2
  • 20
  • 37
  • Possible duplicate : http://stackoverflow.com/questions/7789521/how-to-link-external-javascript-file-onclick-of-button – Varit J Patel Jul 08 '16 at 08:51
  • Why are you adding it on button click instead of simply adding to the page and using the button click to run the functions within the script? – StudioTime Jul 08 '16 at 08:51
  • When it is just added to the page it just opens as soon as the page opens. I wanted to be able for the user to request it by clicking on a button then it should appear – StudentRik Jul 08 '16 at 08:53
  • @StudentRik Then why don't you execute that script on button click instead of dom ready. – Jai Jul 08 '16 at 08:55
  • How would I do that? if it is just a url – StudentRik Jul 08 '16 at 08:56

1 Answers1

1

You need to add the protocol to make it a proper URL in this case.

I would also add it to the head instead of body unless the script document.writes stuff :

function loadPopUp() {
    var script = document.createElement('script');
    script.src = location.protocol+'//r1.dotmailer-surveys.com/scripts/popover/111111‌​?a=uc';
    var head = document.getElementsByTagName("head")[0];
    head.appendChild(script);
    console.log("clicked");
    return false;
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236