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

5
  • Possible duplicate : stackoverflow.com/questions/7789521/… Commented Jul 8, 2016 at 8: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? Commented Jul 8, 2016 at 8: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 Commented Jul 8, 2016 at 8:53
  • @StudentRik Then why don't you execute that script on button click instead of dom ready. Commented Jul 8, 2016 at 8:55
  • How would I do that? if it is just a url Commented Jul 8, 2016 at 8:56

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

It is adding the correct script to the page now. But does not show the popup it adds an iframe but doesn't fire the popup opening.
I cannot tell you without seeing the script it creates
Thanks for your help. I will get onto dotmailer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.