Search code examples
javascriptsharepoint-2013

I'm trying to centralize my js scripts so that I can re-use on multiple pages


Example 1: I have a js script that I have in a script editor to wrap my promoted links. I want to replace this with a reference to a js script I will place in the Site Assets and passing one parameter that equals the number of links per row.

So I moved my code into the Site assets and reference it using the following and it did not seem to work. I am using a script editor. Not passing any parameters yet.

<script type="text/javascript" src="../Site%20Assets/js-enterprise/WrapPromotedLinks.js"></script>

My code in my Site Assets is:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js Jump "></script>
<script type="text/javascript">
$(document).ready(function () {

// Update this value to the number of links you want to show per row
var numberOfLinksPerRow = 3;
alert(numberOfLinksPerRow);   

// local variables
var pre = "<tr><td><div class='ms-promlink-body' id='promlink_row_";
var post = "'></div></td></tr>";
var numberOfLinksInCurrentRow = numberOfLinksPerRow;
var currentRow = 1
// find the number of promoted links we're displaying
var numberOfPromotedLinks = $('.ms-promlink-body > .ms-tileview-tile-root').length;
  // if we have more links then we want in a row, let's continue
  if (numberOfPromotedLinks > numberOfLinksPerRow) {
    // we don't need the header anymore, no cycling through links
    $('.ms-promlink-root > .ms-promlink-header').empty();
    // let's iterate through all the links after the maximum displayed link
    for (i = numberOfLinksPerRow + 1; i <= numberOfPromotedLinks; i++) {
      // if we're reached the maximum number of links to show per row, add a new row
      // this happens the first time, with the values set initially
      if (numberOfLinksInCurrentRow == numberOfLinksPerRow) {
        // i just want the 2nd row to
        currentRow++;
        // create a new row of links
        $('.ms-promlink-root > table > tbody:last').append(pre + currentRow + post);
        // reset the number of links for the current row
        numberOfLinksInCurrentRow = 0    }    
// move the Nth (numberOfLinksPerRow + 1) div to the current table row    
$('#promlink_row_' + currentRow).append($('.ms-promlink-body > .ms-tileview-tile-root:eq(' + (numberOfLinksPerRow) + ')'));    
// increment the number of links in the current row
    numberOfLinksInCurrentRow++;  }
}
});
</script>

I want to keep a reference only on my page passing in the parameter 3 for now.


Solution

  • Follow the steps below to achieve it.

    1.Save the code below as js file "WrapPromotedLinks.js".

    $(document).ready(function () {
        // Update this value to the number of links you want to show per row
        var numberOfLinksPerRow = 3;
        //alert(numberOfLinksPerRow);   
        // local variables
        var pre = "<tr><td><div class='ms-promlink-body' id='promlink_row_";
        var post = "'></div></td></tr>";
        var numberOfLinksInCurrentRow = numberOfLinksPerRow;
        var currentRow = 1
        // find the number of promoted links we're displaying
        var numberOfPromotedLinks = $('.ms-promlink-body > .ms-tileview-tile-root').length;
        // if we have more links then we want in a row, let's continue
        if (numberOfPromotedLinks > numberOfLinksPerRow) {
            // we don't need the header anymore, no cycling through links
            $('.ms-promlink-root > .ms-promlink-header').empty();
            // let's iterate through all the links after the maximum displayed link
            for (i = numberOfLinksPerRow + 1; i <= numberOfPromotedLinks; i++) {
                // if we're reached the maximum number of links to show per row, add a new row
                // this happens the first time, with the values set initially
                if (numberOfLinksInCurrentRow == numberOfLinksPerRow) {
                    // i just want the 2nd row to
                    currentRow++;
                    // create a new row of links
                    $('.ms-promlink-root > table > tbody:last').append(pre + currentRow + post);
                    // reset the number of links for the current row
                    numberOfLinksInCurrentRow = 0;    
                }    
                // move the Nth (numberOfLinksPerRow + 1) div to the current table row    
                $('#promlink_row_' + currentRow).append($('.ms-promlink-body > .ms-tileview-tile-root:eq(' + (numberOfLinksPerRow) + ')'));    
                // increment the number of links in the current row
                numberOfLinksInCurrentRow++;  
            }
        }
    });
    

    2.Upload the file into the folder "js-enterprise" in Site Assets library.

    3.Use the references below in script editor web part in the SharePoint page to make it works.

    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="../SiteAssets/js-enterprise/WrapPromotedLinks.js"></script>