Can only execute script one time

1.1k Views Asked by At

I'm using Ajax to load a page, and on that page I'm loading an external script from issuu.com. It works very nicely the first time either I or Ajax load the page, but if Ajax loads a new page and then goes back again it will not run the external script again (It loads the script, but doesn't execute it).

This is the page: http://dymak-corporate.134.lait.dk/catalogue/

You can see in e.g. the network tab of your browser's dev tools that it loads the script both times, but only executes the first time.

I have tried to load and execute the script in every way I can think of and nothing is working.

jQuery GetScript:

 $.getScript("//e.issuu.com/embed.js")

Ajax load the script and eval it:

$.ajax({
    url: "/scripts/embed.js",
    success: function (responseText) {
        eval(responseText);
    }
});

Adding it to the header every time and removing it before I add it:

$('#issuuScript').remove();
var s = document.createElement("script");
s.id = "issuuScript";
s.src = "//e.issuu.com/embed.js";
document.head.append(s);

Reloading it at every load:

function reload_js(src) {
    $('script[src="' + src + '"]').remove();
    $('<script>').attr('src', src).appendTo('head');
}
reload_js('/scripts/embed.js');

I also tried adding it inline, without success.

2

There are 2 best solutions below

1
On

This is not how JavaScript is supposed to work. A file include is not a like a function call, but you are treating it like a function call.

How you should think about it:

I want to call some_module.some_function(). Is some_module loaded?

No. Load the file that has the module, then execute the function.

Yes. Just execute the function.

0
On

I ended up just using the iframe implementation from issuu