0

I am using smoothState.js to prefetch a page through ajax. When it updates the page with the html it loads the javascript file again.

The page then seems to run both causing errors. Can I stop jQuery loading the script?

    /** Run when requested content is ready to be injected into the page  */
    onEnd : {
        duration: 0,
        render: function (url, $container, $content) {
            $body.css('cursor', 'auto');
            $body.find('a').css('cursor', 'auto');
            $container.html($content);
        }
    },

The script in the html that that it loads a second time after the ajax load

<script src="/js/main.min.js" async defer="defer"></script>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Keith Power
  • 13,891
  • 22
  • 66
  • 135

1 Answers1

1

if i understand correctly, try this:

var stop;


/** Run when requested content is ready to be injected into the page  */
onEnd : {
    duration: 0,
    render: function (url, $container, $content) {
        // the id #content must be after script tag in your page
        if(stop) $content = jQuery($content).find('#content').html(); 
        $body.css('cursor', 'auto');
        $body.find('a').css('cursor', 'auto');
        $container.html($content);
        // after first request enable stop variable
         stop = true;
    }
},

html:

<script src="/js/main.min.js" async defer="defer"></script>

<div id="content">.........</div>
vaneayoung
  • 353
  • 1
  • 4
  • 22
  • sorry I am still getting the error Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects ... the site is http://hotelieracademy.com when the cursor hovers the menu link the page is prefetch but when the page is updated with the new html it loads the javascript a second time and tries to run it – Keith Power Mar 08 '15 at 23:49
  • 1
    thank you. Looking at your solution I moved the javascript to the head to be outside the body and it now works. – Keith Power Mar 09 '15 at 00:15