0

i like to do a simple javascript execution with MDS and without MDS.

To do this, I have used this Javacript only executed on first page load HowTo to get work.

The Problem i have, is that the script only is run once time. With a MDS Delta and a direct invoke.

/sites/sitecollection/_layouts/15/start.aspx#/SitePages/test.aspx
/sites/sitecollection/SitePages/test.aspx

Both link works great for one time. When i now go to home and back to the test link, my script will not run.

Here are my scripts:

TEST.ASPX

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<meta name="CollaborationServer" content="SharePoint Team Web Site" />
<SharePoint:ScriptBlock runat="server">
<SharePoint:ScriptLink language="javascript" name="~site/SiteAssets/jquery.min.js" OnDemand="false" runat="server" Localizable="false"/>
<SharePoint:ScriptLink language="javascript" name="~site/SiteAssets/mycode.js" OnDemand="false" runat="server" Localizable="false"/>
var navBarHelpOverrideKey = "WSSEndUser";
</SharePoint:ScriptBlock>
<SharePoint:RssLink runat="server" />
</asp:Content>

MYCODE

function myCode(){
// jQuery code goes here
// you can use $ namespace

    alert("Hello");
}
function preCode(){
      Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(myCode);
}
if(typeof _spPageContextInfo != "undefined" && _spPageContextInfo != null){

     var jsURL = _spPageContextInfo.siteServerRelativeUrl + "/SiteAssets/mycode.js";
     RegisterModuleInit(jsURL, myCode());
}else{
     ExecuteOrDelayUntilScriptLoaded(preCode, "sp.init.js"); 
}

Can anyone tell me my fault? I am frustrating.

Thanks for all your help.

0

The whole essence of MDS is not to load (and execute) the same scripts again.

You have to hook into the asyncDeltaManager to tell MDS you want something executed beside the initial load.

asyncDeltaManager.add_beginRequest(function(){
  //run on every MDS request
});

There are more MDS events you can hook into: pageLoad, pageLoading, endRequest

Note that with MDS anything you add to the DOM will not get deleted on page transitions

Note 2: You can add jQuery inside the JSLink setting, you can separate multiple JS file references with a | (pipe) symbol

  • Hey Danny, thanks for your quick reply. Where i have to embed the asyncDeltaManager? In the function mycode() ? – user25772 May 9 '16 at 12:40
  • It is the same concept as the PageRequestManager you already use. – Danny '365CSI' Engelman May 9 '16 at 12:42
  • When i am using the jslink, sharepoint only execute it once time. So i got the same problem again. Or do i have to use a specific function from sharepoint? – user25772 May 10 '16 at 6:34
  • MDS will execute any JS code only once, the brute force method is to disable the MDS feature in Site Settings – Danny '365CSI' Engelman May 10 '16 at 7:28

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.