1

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.

user25772
  • 15
  • 5

1 Answers1

1

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

Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79