0

I have added the following code after the include for the JQuery file and the script tags in my master page. I put the script immediately before the end body tag of the master page.

        $(document).ready(function () {
        $(".ms - draggable ").each(function () {
            alert("running");
            var alt = $(this).attr('alt');
            alt = "../"+ alt;
            var a = $('<a/>').attr('href', alt);
            $(this).wrap(a);
        });
        alert("docuemnt ready");
    });
    alert("running");

After I add the code. I save the page. Refresh my SharePoint site and I get the alerts only once. When I change pages or refresh the same page, I do not get the alerts again. What can I do on my master page to ensure this script executes after every page loads?

UPDATE: So this has morphed into this post as I realized they are both related to the same thing.

Top Naviation Menu is not loaded when script runs - SharePoint Foundation 2013 Oslo Master Page

  • You must try to debug the script, may be by adding debugging point or adding 'debugger;' in the code. – Amit Aug 6 '17 at 8:05
  • I already did that - see the alert statements? that is how I know the script only executes once. The running alert should be triggered whenever the script runs. The others are only triggered after the document ready function executes. None of the alerts are being presented more than once. – George M Ceaser Jr Aug 6 '17 at 15:51
2

Ensure the class for other pages exists. You may console (in debugging tools of browser) the length of the selector using the below if it is greater than 0.

$(".ms - draggable ").length

Also, I believe the class name is not correct. Spaces between the class name will make it act as 2 different classes.

  • That is not the problem. My alerts are not firing that is how I know the script only executes once. – George M Ceaser Jr Aug 6 '17 at 15:51
0

Can you check if the Minimal Download Strategy Site Feature is activated by going to Site Settings -> Site Features . If it is activated, can you please try deactivating it and check the jQuery functionality.

enter image description here

The reason to check if MDS is enabled is,MDS does not do a Full page load on subsequent requests . Code written within jQuery $(document).ready() {} method rely on the DOM events when the page loads, so it might work perfectly fine on your first page load when MDS is enabled, since we need to fetch the full page, but on subsequent page transitions the JavaScript will not fire.

An MDS page transition does not fire any DOM document load events since it asynchronously partially updates the page.

Detailed Reference

If deactivating MDS solved the jQuery issue, You have 2 options

  • Choose to deactivate it for the future(which is not really recommended as its a new feature that was brought in for performance improvement)
  • You can use the RegisterModuleInit method as said in the reference link to work around the issue(so that the script works with MDS)

SE Reference

  • I am using SharePoint Foundation 2013 and the Minimal Download Strategy Feature is not list as a site feature. What you are explaining is what I thought was happening though. I will look at the reference you provided to see if I can figure out a solution. – George M Ceaser Jr Aug 6 '17 at 15:58
  • 1
    I just found where MDS is in the site and it is not Active. – George M Ceaser Jr Aug 6 '17 at 22:32
  • Correction - Gosh If I could read --- ARG! The Minimal Download Strategy WAS enabled.. I disabled it and my script fired all the time. I will look at the reference to see if I can use the registermoduleInin method to work around the MDS issue, – George M Ceaser Jr Aug 14 '17 at 1:22
  • So please see the new link in the question as I am now trying to use the RegisterModuleInit method but it does not seem to work on a master page. sharepoint.stackexchange.com/questions/223069/… – George M Ceaser Jr Aug 16 '17 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.