9 Replies Latest reply on Feb 28, 2008 3:58 PM by pmn92

    How can I to include JavaScript files in Portlets to JBoss P

    Santiago Alonso Newbie

      Hello, my question is described in the subject of this post. Thank you.

        • 1. Re: How can I to include JavaScript files in Portlets to JBo
          pmn92 Novice

          You can write into the content any well formed html and thus javascript stuff.
          You can define your own layout, usually a jsp file - again html stuff.
          You can inject html into teh <header-content> tag in the jboss-portlet.xml file

          • 2. Re: How can I to include JavaScript files in Portlets to JBo
            Santiago Alonso Newbie

            Hello, I am writting the view layer of my Portlets in JSP files. If I write the JavaScript directly into my JSP file, I can to call JavaScript functions correctly.

            I want to include this JavaScript functions in a external file in my project. This file must be into the war file of the Potlet.

            I want to call this functions from the external JavaScript file into my JSP file.

            My problem... I don't know how to do this.

            • 3. Re: How can I to include JavaScript files in Portlets to JBo
              Dirk vanWestrienen Newbie

              One solution:

              Include your JavaScript in a file and place in the web root of your project. Lets assume the project has a context root of /MyProject. Then in your JSP, send back the following along with your other output:

              <script type="text/javascript" src="/MyProject/myjavascript.js"></script>
              


              Now you should be able to reference your JavaScript code in your portlet.

              • 4. Re: How can I to include JavaScript files in Portlets to JBo
                Santiago Alonso Newbie

                Well, this solution works fine, but only with the first Portlet in the Portal.

                I have 2 Portlets in the Portal. Both uses this method, but only works with the first. If I invertered the order of aparition of the Portlets, now works well with the Portlet that before was the second in the view, and that now is the first.

                The second Portlet in a view in the Portal, it can not to interact with the JavaScript, but looking the source, the inclusion of the file has been correct.

                Any solution?. Thank you.

                • 5. Re: How can I to include JavaScript files in Portlets to JBo
                  Thad West Newbie

                  So you are having a single portlet download common javascript files that are used by multiple portlets? That sounds like a bad idea....If this is truly common js code, why not have the theme load it for you? Or reference it in every portlet, the browser will be smart enough to cache it for you.

                  If you are set on your current structure, my guess is that your other portlets that are trying to execute the javascript should tap into the onload event and start processing then.

                  • 6. Re: How can I to include JavaScript files in Portlets to JBo
                    je le Newbie

                     

                    "swisst" wrote:
                    So you are having a single portlet download common javascript files that are used by multiple portlets? That sounds like a bad idea....If this is truly common js code, why not have the theme load it for you? Or reference it in every portlet, the browser will be smart enough to cache it for you.

                    If you are set on your current structure, my guess is that your other portlets that are trying to execute the javascript should tap into the onload event and start processing then.


                    that would mean his portlet would be link to a specific theme. Change the theme, the all site go down :-)
                    btw, if you add all js in the theme -and when working with ajax/mootools it can result by adding >20>50>100 js files ! (ok, on a really big fancy site...)
                    I understand what dickinson try to do because, well, I'm doing the same. :-)

                    You cannot relly on the cache neither : every time the browser found a , it runs it, that a major problem. not only it's time consuming, but it could create some js exception.

                    A solution is to add script, that load script :-) basically something like :

                    <div id="<portlet:namespace/>_scripts"></div>
                    <script type="text/javascript">
                    if (typeof MooTools == "undefined") {
                     var script = document.createElement("script");
                     script.src = "${renderRequest.contextPath}/js/mootools.js";
                     script.type= "text/javascript";
                     var <portlet:namespace/>_scripts = document.getElementById("<portlet:namespace/>_scripts");
                     <portlet:namespace/>_scripts.appendChild(script);
                    }
                    </script>
                    


                    I almost finished a little script that make the process less painfull.

                    • 7. Re: How can I to include JavaScript files in Portlets to JBo
                      je le Newbie

                      oops !!!!!!!!! look like someone eat my lines :-D

                      Correct sentence is :


                      You cannot relly on the cache neither : every time the browser found a <script>, it runs it, that a major problem. not only it's time consuming, but it could create some js exception.

                      A solution is to add script, that load script :-) basically something like (see post upper)


                      • 8. Re: How can I to include JavaScript files in Portlets to JBo
                        Thad West Newbie

                        Yeah, linking to a specific theme sounds like a bad idea as well!

                        I guess my point is that I have always treated each portlet as a unique entity, so the thought of one portlet being in charge of loading all of the JS for other portlets just feels really wrong.

                        For example, we use some JS libraries in our application, almost every portlet uses them, and they include the files in their jsp. We have taken the position that each portlet should be able to stand on its own, and we don't want to write a bunch of framework code to handle duplicate js files.

                        I guess that is an interesting question though, we better be sure that the JS namespace is not polluted with multiple functions of the same name. Additionally, different versions of the same library could cause issues as well. Even though our portlets stand on their own, the end javascript is all mingled together and processed by the browser. Maybe that's the reason for the JBoss javascript injection code!

                        • 9. Re: How can I to include JavaScript files in Portlets to JBo
                          pmn92 Novice

                          In that case, where you do not want to control the layout of the page, the only choice left is to use an ajax dev kit that allow to dynamically load resourcces and dependencies for you (scripts and css) - I recommend you the YUI dev kit : 2.5 is perfect for that.