External scripts .js inside a page working only first time

In my case for instance any JS scripts added to the called page is working first time when entering the page, after going back and calling that page again the scripts in general do not work or run anymore and no errors in the console.

I tried also with facebook SDK JS putting the code inside the pageInit or using the code below
and it runs the code one time and then when going back to entering the page again the script is not called anymore or not working…

any ideas for fix this? in all pages happen the same when you need to have a script that only works for that page…

I know I can put the scripts in the index page but Facebook sdk needs to access the DOM elements when the JS is added and there is no elements yet until the page containing the login screen is called…

my code :

pageInit: function(e, page) {
console.log(‘pageInit’, page);

        var self = this;
        var app = self.$app;
        var fruits = self.fruits;
        var $ = self.$;
        var s = document.getElementsByTagName('script')[0];
        var sc = document.createElement('script');
        sc.type = 'text/javascript';
        sc.async = false;
        sc.src = 'js/fb.js';
        s.parentNode.insertBefore(sc, s);

only refreshing the page make the script working again but only run one time… for that page not the second time I call the page :frowning: any solutions ? thanks

Never do such things, include external script files only once and only in index file.

There is a Facebook API to parse newly loaded elements to DOM

Thank you I tried to do this including all in index file but the problem is that facebook API does not work there some elements needed are not ready and the console.log give me the error…
I will try to look a fix around to make it work… if facebook API allows me to parse newly loaded elements it will be great but in my case it gives me error if the facebook sdk js is in the index…

I tried to call the facebook login screen on popup it gives me error,
I tried to call the facebook login screen on a page it gives me error,

If I include the js in a that page onPageinit it gives me no error and works but second time it is called that page it is not running anymore…

I think the only solution it is to put the facebook login in the main screen… in pages or popup pages it does not work in my case…

any other solution?