<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>References for using javascript in page fragments</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=119480888" />
  <subtitle>References for using javascript in page fragments</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=119480888</id>
  <updated>2026-04-04T23:12:13Z</updated>
  <dc:date>2026-04-04T23:12:13Z</dc:date>
  <entry>
    <title>RE: References for using javascript in page fragments</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=119488730" />
    <author>
      <name>Christoph Rabel</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=119488730</id>
    <updated>2020-06-25T14:49:20Z</updated>
    <published>2020-06-25T14:49:20Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Examples: You can look into the standard Liferay fragments, the basic components Slider or Tabs contain Javascript.&lt;br&gt;The Javascript code in the Javascript window is encapsulated inside of a function and therefore hidden. That's actually good, very good because the stuff in there would otherwise pollute the global namespace.&lt;br&gt;There are numerous tutorials out there explaining that practice, e.g.&lt;br&gt;&lt;a href="https://www.tutorialspoint.com/javascript-encapsulation-using-anonymous-functions"&gt;https://www.tutorialspoint.com/javascript-encapsulation-using-anonymous-functions&lt;/a&gt;&lt;br&gt;There are two ways around this, one as Liferay does it, attaching events to objects/buttons and so on in that unnamed function. The other way, that also makes sense in some usecases, is to add your functions to the window object.&lt;br&gt;&lt;pre&gt;&lt;code&gt;window.foo = function() { ... }&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;I would advise to use the first way, if possible.&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Christoph Rabel</dc:creator>
    <dc:date>2020-06-25T14:49:20Z</dc:date>
  </entry>
  <entry>
    <title>References for using javascript in page fragments</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=119480887" />
    <author>
      <name>John Cressman</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=119480887</id>
    <updated>2020-06-24T20:05:42Z</updated>
    <published>2020-06-24T20:05:42Z</published>
    <summary type="html">I am trying to create a dynamic page fragment and need to use some javascript.  I see the area in the fragment design screen where I can put javascript, but I can&amp;#39;t seemed to reference it at all from inside the HTML area.  Nothing works - even when I call a simple function with an alert box.I looked through the site at the instructions to create fragments and found the javascript description EXTREMELY lacking.  Basically it says don&amp;#39;t write alot of javascrript, call external JS Libraries.  Can anyone point me to some sample fragments where I see how they are calling the JS?</summary>
    <dc:creator>John Cressman</dc:creator>
    <dc:date>2020-06-24T20:05:42Z</dc:date>
  </entry>
</feed>
