Hey all, got a tricky one for you.

I have an RSS web part which uses a custom ItemStyle.xsl stylesheet to transform the results of the RSS feed into a UL list.

I have some jQuery I want to inject into the page via this xsl (a jQuery plugin, to be more precise), but I need to physically call this function once the RSS web part has asynchronously did its job, got the XML, put it through the XSL and spat the output into the browser.

I know I probably should load the jQuery contents in the masterpage, but I would still have the issue that I want to call the jQuery function after the XSL has loaded and populated the web part.

The code looks a bit like this:

  <xsl:template match="RSSMainTemplate">
      <div id="container">
        <ul id="a_list">
          <xsl:apply-templates select="channel/item">
            <!-- got a template here to render the LI tag for each item in the RSS feed -->
          </xsl:apply-templates>
        </ul>
      </div>
    </div>

    <!-- Here's the issue, how do I get this to kick off? This is brought into the page & loaded into the DOM after what could be many seconds after the rest of the page has loaded, as the RSS web part gets its source XML asynchronously -->
    <script type="text/javascript" language="javascript">
      <![CDATA[

          $(function(){
              $("ul#a_list").doStuff();
          });
          ]]>
    </script>
  </xsl:template>

Any ideas?

up vote 5 down vote accepted

Managed to get it working - had to do this in the XSL:

<script language="javascript" defer="true">
      <xsl:comment>
        <![CDATA[
         $(document).ready( function() {
            alert('boo');
         });
        ]]>
      </xsl:comment>
    </script>
  • must be annoying with that alert popping up all the time to get it working tho :-p – Anders Rask Feb 24 '11 at 23:10
  • 1
    Hey, I did a SharePoint Dance when it finally came up :P – James Love Feb 25 '11 at 8:21
  • This solution is compatible only on IE. – Domenico Aug 26 '13 at 16:25
  • I need to add first a empty set of script tags, and in the second one do the actual code, like: <script language="javascript" defer="defer"> /* */ </script> <script language="javascript" defer="defer"> $(function() { SetupSearchWebParts(); }); </script> – Jussi Palo Jan 17 '14 at 7:41

In a similar situation for me the key was to set "defer" parameter. I think the right way is

<script type="text/javascript" defer="defer">
</script>

The above solutions do not work after SP2010. A less than elegant solution is to use the onerror handler of an img tag:

<img style='display:none' src='#' onerror='console.log("boo!")' />

http://paulryan.com.au/2017/excuting-js-in-an-rss-viewer-web-part-in-sharepoint/

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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