Foren

javascript in portlets

Vasanth Jayabalan, geändert vor 12 Jahren.

javascript in portlets

New Member Beiträge: 24 Beitrittsdatum: 25.02.11 Neueste Beiträge
Greetings,

I have four portlets in a liferay page. I include the same javascript file in the header of each of these portlets using the <header-portlet-javascript> tag in the liferay-portlet.xml file. Does this mean that each of these portlets get their own copy of this javascript file?...or is there just one global file for the entire page?

thanks,
Vasanth
thumbnail
Leo Pratlong, geändert vor 12 Jahren.

RE: javascript in portlets

Expert Beiträge: 363 Beitrittsdatum: 06.07.10 Neueste Beiträge
I think you'll have as JS file as the number of portlet which declares it in the liferay-portlet.xml .

It's better to delegate to a theme the loading of the JS files.
Vasanth Jayabalan, geändert vor 12 Jahren.

RE: javascript in portlets

New Member Beiträge: 24 Beitrittsdatum: 25.02.11 Neueste Beiträge
Leo,

I looked at the source for the portal page, and the js file was included only once in the <script> tag. So I guess, it's global to the page. The theme option you suggested looks interesting. I'll try that shortly.

I have another question. Let's say there are two portlets in a page and they have the same names and ids for the DIV elements in them. They both call the same javascript function to update the DIV. Now which DIV will get updated? I think we have to use portlet namespaces to solve this problem. I haven't used them...have you?

Vasanth
thumbnail
jelmer kuperus, geändert vor 12 Jahren.

RE: javascript in portlets

Liferay Legend Beiträge: 1191 Beitrittsdatum: 10.03.10 Neueste Beiträge
Yes,you should use namespaces. You should simply prepend <portlet:namespace/> to your ids and js functions
Vasanth Jayabalan, geändert vor 12 Jahren.

RE: javascript in portlets

New Member Beiträge: 24 Beitrittsdatum: 25.02.11 Neueste Beiträge
That makes sense. So if I have two portlets on a page that use a common function (say function myFunc()), the javascript will eventually have two functions like function ads_hgj_myFunc() and tut_hdr_myFunc(), where the garbage in front of the function name in this example is the namespace?
thumbnail
Leo Pratlong, geändert vor 12 Jahren.

RE: javascript in portlets

Expert Beiträge: 363 Beitrittsdatum: 06.07.10 Neueste Beiträge
For the IDs, it depends on your portlet.
With a Liferay Struts or MVC portlets, you'll need to use portlet:namespace. But I develop JSF portlet, and I do not need to put the portlet id on my ID attributes. Liferay (or JSF ?) auto-writes the portlet ID.
It's great, but there is a problem with JS code : so I use jQuery selector to find my elements with ID (for example: $('input[id*=foo]')). Because I do not want to import liferay "portlet" taglib in my "xhtml" files.