JS Example

Javascript Examples

By placing custom javascript code in Footer JS placement you can extend the application functionality.

Note that you should not place <script></script> tag in the Footer JS. The code you put there is directly inserted within the script block.

Place your code within an anonymous function and we also suggest to add try...catch to not cause any accidental javascript error which would prevent the LMS pages to render completely. In case you break the script and are unable to edit the content back, here is how to rectify it.

  • Open accounts.edubrite.com and use site owner's credential to login
  • You would see the sites you own there
  • Edit the site details where you were working for customization, and add DISABLE_PLACEMENTS=true in the customization tab, and save
  • Now reopen the original microsite, you would notice your customizations done via placements are not visible anymore. Fix the javascript and then when ready, remove the DISABLE_PLACEMENTS=true property

Here are some examples of using javascript Ajax calls from the footer JS

1. Fetching the groups in which current user is a member

(function (){
    var dataObj = { "dispatch": "list", "json": "true", "searchIn": "GROUP", "pageSize": 20 };
    $.post( "../site/search.do",
            dataObj,
        function( data ) {
            //Do something
        }, "json"
    );
    myGroupsFetched = true;
})();

Check the response in your site by entering this URL in browser

http://<<your-site-url>>/oltpublish/site/search.do?dispatch=list&searchIn=GROUP&pageSize=20&json=true

2. Fetch courses enrolled by the current user

(function (){
    var dataObj = { dispatch: "listCourseSessions", fullDetails:true, json: true, view: "USER", pageSize: 10, award: false };

/*optional parameters - group id, course name to search, course session name to search
    dataObj.groupId = "...";
    dataObj.courseName = "...";
    dataObj.sessionName = "..."; */
 
    $.post( "../site/program.do",
            dataObj,
        function( data ) {

//Do something
        }, "json"
    );
})();

Check the response in your site by entering this URL in browser

 

http://<<your-site-url>>/oltpublish/site/program.do?dispatch=listCourseSessions&fullDetails=true&view=USER&pageSize=10&award=false&json=true

3. Adding custom content in Dashboard

(function(){

if(location.href.indexOf('homeNew.do') != -1){
$("#maincenter").prepend("<div style=''>Hello</div>");
}

})();

4. In the above example, checking if the current user is learner, instructor or admin

(function(){

   if(eb_IsGuest){

       //Not logged in

   }else if(eb_IsAdminSiteAdmin){

      //Site admin or owner

   }else if(eb_IsTeacher){

      //Instructor

   }else{

      //Learner

      if(location.href.indexOf('homeNew.do') != -1){
         $("#maincenter").prepend("<div style=''>Hello</div>");
      }

   }
})();



 

 


Rating: