2

I have 4 sets of Promoted Links in a table. The idea being the top set show one of the others depending on which tile is clicked.

I have slightly modified THIS SCRIPT to match my needs, but nothing happens.

I have inserted the below into a Script Editor webpart. What am I missing?

EDIT - line 10 - 13 are bothering me...

<script type="text/javascript" src="/Scripts/JQuery/jquery-1.8.1.js"></script>
<script language="javascript">

$(document).ready(function() {

$("div#WebPartWPQ3").hide();
$("div#WebPartWPQ4").hide();
$("div#WebPartWPQ5").hide();
});
    $(function () {
    $("a[id^=Tile_WPQ]").each(function () {
            $(this).click(function () {
                var linkTitle = $(this).find('li[title]').attr('id');
                if (linkTitle == "Tile_WPQ2_1_7")
                {
                    $("div#WebPartWPQ3").show();
                    $("div#WebPartWPQ4").hide();
                    $("div#WebPartWPQ5").hide();
                    event.returnValue = false;

                }

                if (linkTitle == "Tile_WPQ2_2_7")

                {
                    $("div#WebPartWPQ3").hide();
                    $("div#WebPartWPQ4").show();
                    $("div#WebPartWPQ5").hide();
                    event.returnValue = false;
                }

                if (linkTitle == "Tile_WPQ2_3_7")

                {
                    $("div#WebPartWPQ3").hide();
                    $("div#WebPartWPQ4").hide();
                    $("div#WebPartWPQ5").show();
                    event.returnValue = false;
                }

            });
        });
    });
</script>
4
  • Are the minimal download strategy feature activated on the site? Commented Jul 9, 2015 at 11:29
  • Make sure the JQuery is loaded in the page. You can also check the console logs for any error
    – Unnie
    Commented Jul 9, 2015 at 11:30
  • Im getting somewhere... part of the proplem was the wrong tile ID (doh!!) Now when i click, the relevant promoted links show exacly where you would expect to but then go straight off the the Promoted Links page. It's almost as if i need to over-ride the launch behavouir or something but the promoted links require this to be sut up!
    – SimonW
    Commented Jul 10, 2015 at 7:38
  • SORTED. All this was, was the Promoted Links launch behaviour needing to be set to "New Tab" - no new tab is actually launched so fits my bill a treat
    – SimonW
    Commented Jul 28, 2015 at 13:13

5 Answers 5

2

This is an example from one of my scripts:

_spBodyOnLoadFunctionNames.push("HideWP");
function HideWP()   
    {   
        //Function body 
    } 

"HideWP" being a wrapping function for the rest of the script..

Try this....

<script language="javascript" type="text/javascript">   
_spBodyOnLoadFunctionNames.push("pageLoad");
  function pageLoad(sender, args)   
    {     
      $("div#WebPartWPQ3").hide();
      $("div#WebPartWPQ4").hide();
      $("div#WebPartWPQ5").hide();

      $(function () {
      $("a[id^=Tile_WPQ]").each(function () {
            $(this).click(function () {
                var linkTitle = $(this).find('li[title]').attr('id');
                if (linkTitle == "Tile_WPQ2_1_7")
                {
                    $("div#WebPartWPQ3").show();
                    $("div#WebPartWPQ4").hide();
                    $("div#WebPartWPQ5").hide();
                    event.returnValue = false;

                }

                if (linkTitle == "Tile_WPQ2_2_7")

                {
                    $("div#WebPartWPQ3").hide();
                    $("div#WebPartWPQ4").show();
                    $("div#WebPartWPQ5").hide();
                    event.returnValue = false;
                }

                if (linkTitle == "Tile_WPQ2_3_7")

                {
                    $("div#WebPartWPQ3").hide();
                    $("div#WebPartWPQ4").hide();
                    $("div#WebPartWPQ5").show();
                    event.returnValue = false;
                }
          });
        });
      });
    } // End pageLoad  
</script>

Also note that the following would work too, replace
_spBodyOnLoadFunctionNames.push("pageLoad"); with
ExecuteOrDelayUntilScriptLoaded(pageLoad, "sp.js");

2
  • at least 2 people agree with this, only i don't know enough about js to where exactly to insert this line and/or what else to remove from my original script! Help please :-)
    – SimonW
    Commented Jul 13, 2015 at 8:16
  • Great, thanks. I had to tweak it a little (tile IDs mainly) and it sort of works. When I click the top Promoted link, for a split second the lower one displays (as expected) but the opens up the lower Promoted Links in a seperate page. How do i modify this behaviour? All your help is much appreciated.
    – SimonW
    Commented Jul 13, 2015 at 16:04
0

I've faced this issue before. You can use:

$(window).load(function() {
  // Your Javascript here
});

It seems that the page is ready before SharePoint load or render all the "SharePoint elements" inside.

4
  • Still no action..
    – SimonW
    Commented Jul 9, 2015 at 13:26
  • Can you use the debugger? In Chrome press f12 and click on the console tab just to check if you have errors in the js
    – yngrdyn
    Commented Jul 9, 2015 at 13:57
  • Great idea! The debugger says "SCRIPT5009: '$' is undefined" and points at line 620, which is $(window).load(finctioon() {
    – SimonW
    Commented Jul 9, 2015 at 14:26
  • adding this to the top <script src="ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.js"></…> now hides the bottom three promoted links, as i was hoping but clicking on the top row only opens them in a new page
    – SimonW
    Commented Jul 9, 2015 at 14:35
0

Try this:

ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
function init(){
    // JavaScript code here
}
2
  • adding that then made all promoted links visible, as if no conde was applied to the page.
    – SimonW
    Commented Jul 9, 2015 at 15:19
  • Do you have originalid as one of the attributes?
    – Keerthi
    Commented Jul 9, 2015 at 15:38
0

Don´t use jquery´s( document).ready function in SharePoint. Use _spBodyOnLoadFunctionNames or something like that. Because the Rendering of SharePoint-Contents happens most using Javascript also AFTER Body loaded, so in your case, even if document.ready fired adequately, the desired Objects in the dom may not have been rendered already

Finally, as I don´t know how your html is built of, I can´t help you any further.

But hopefully you included the jquery-file in the correct Folder of your SharePoint-Server.

3
  • it would maybe help if you make an example of how to use _spBodyOnLoadFunctionNames
    – Gwny
    Commented Jul 9, 2015 at 11:28
  • _spBodyOnLoadFunctionNames.push("<NameofyourFunction>"). This ensures the execution of the Function with that Name after SharePoint has loaded and rendered completely. Commented Jul 9, 2015 at 11:45
  • so replace "$(document).ready(function()" with "_spBodyOnLoadFunctionNames"? excluding the " "
    – SimonW
    Commented Jul 9, 2015 at 12:19
0

Below is the complete template I use when adding script to my webpages, granted I am throwing this in a CEWP and pointing to the script that way. I don't inject my code into the page itself. Hopefully you can glean something from this....

<SharePoint:ScriptLink Name="MicrosoftAjax.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.core.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.js" runat="server" Defer="True" Localizable="false"/>
<SharePoint:FormDigest ID="MyFormDigest" runat="server"/>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.min.js"></script>
<script language="javascript" type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(pageLoad, "sp.js");
  function pageLoad(sender, args)   
    {      // Wrapping function
    } // End wrapping 
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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