Vista combinada Visión Plana Vista de árbol
Discusiones [ Anterior | Siguiente ]
toggle
John Nguyen
javascript
31 de julio de 2012 14:49
Respuesta

John Nguyen

Ranking: New Member

Mensajes: 21

Fecha de incorporación: 16 de julio de 2012

Mensajes recientes

I've read so many threads about methods to include javascripts to the site; however I am unable to do this successfully. I've tried it through the liferay-portlet.xml as well as the portal_normal.vm and its not working.

For liferay-portlet.xml, I'm adding this:
1
2<header-portlet-javascript>https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js</header-portlet-javascript>

I'm not quite sure where it is I need to add it in this file, but I've added right before the
1</liferay-portlet-app>


For portal_normal.vm, I added:
1<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

before the
1</head>


Any help would be great,

Thanks!
Hitoshi Ozawa
RE: javascript
31 de julio de 2012 14:55
Respuesta

Hitoshi Ozawa

Ranking: Liferay Legend

Mensajes: 7954

Fecha de incorporación: 23 de marzo de 2010

Mensajes recientes

Just download the js file and put it in your js directory instead of trying to get it from the web site.
Vishal Panchal
RE: javascript
31 de julio de 2012 21:29
Respuesta

Vishal Panchal

Ranking: Expert

Mensajes: 279

Fecha de incorporación: 20 de mayo de 2012

Mensajes recientes

Hi John Nguyen,

As per your post I think u want to add .js file in theme and wanted to use it in your portal_normal.vm file .
If I got you right than following r the steps you may follow.
1) Download the js file
2) in my-theme/docroot/_diffs/js folder, added the file
3) In protal_normal.vm file you can access the js file using following tag
<script src="$javascript_folder/jquery.min.js.js" type="text/javascript" ></script>


Hope It would be helpful to you...


Thanks & Regards,
Vishal R. Panchal
Priyanka Dhingra
RE: javascript
31 de julio de 2012 23:05
Respuesta

Priyanka Dhingra

Ranking: Liferay Master

Mensajes: 501

Fecha de incorporación: 19 de diciembre de 2011

Mensajes recientes

Hi John Nguyen,
You can keep the file in your portlet also
under the docroot->js folder
and use it as
1<script  type="text/javascript" src="/xxx-portlet/js/jquery.min.js"></script>
David H Nebinger
RE: javascript
1 de agosto de 2012 10:40
Respuesta

David H Nebinger

Ranking: Liferay Legend

Mensajes: 7512

Fecha de incorporación: 1 de septiembre de 2006

Mensajes recientes

Priyanka Dhingra:
Hi John Nguyen,
You can keep the file in your portlet also
under the docroot->js folder
and use it as
1<script  type="text/javascript" src="/xxx-portlet/js/jquery.min.js"></script>


Wrong wrong wrong. If you have multiple copies of your portlet on a page, each copy is going to load the script. That is why you put it into the liferay-portlet.xml file, because Liferay will ensure only one copy of the script is loaded on the page, regardless of what portlets on the page are using it.
Priyanka Dhingra
RE: javascript
1 de agosto de 2012 22:37
Respuesta

Priyanka Dhingra

Ranking: Liferay Master

Mensajes: 501

Fecha de incorporación: 19 de diciembre de 2011

Mensajes recientes

Thanks David for correcting me.
But one ques.....Keeping in liferay-portlet.xml is better or keeping in a jsp page like init.jsp...
Since we have many portlets under one project all including the init.jsp.
so, putting in liferay-portlet.xml for each portlet is better or in a common init.jsp of the project???
David H Nebinger
RE: javascript
2 de agosto de 2012 8:51
Respuesta

David H Nebinger

Ranking: Liferay Legend

Mensajes: 7512

Fecha de incorporación: 1 de septiembre de 2006

Mensajes recientes

in liferay-portlet.xml. When the page is being rendered, all of the entries are checked and only one script include will be on the rendered page. Putting it in init.jsp still means that every portlet instance (which has init.jsp pulled in) would still try to reference the same script.
Hitoshi Ozawa
RE: javascript
2 de agosto de 2012 14:29
Respuesta

Hitoshi Ozawa

Ranking: Liferay Legend

Mensajes: 7954

Fecha de incorporación: 23 de marzo de 2010

Mensajes recientes

What David is saying is this, putting it in init.jsp is the same thing as putting it in each of your jsp file because you're actually just including it. If the portlet is instanceable (that is, something like web content portlet that can be placed several times in a page), each instance of that portlet is going to try to load the js file.
If you put it in the liferay-portlet.xml file, liferay will only load the js file once. Thus, the page is going to load faster.
Vishal Panchal
RE: javascript
2 de agosto de 2012 22:28
Respuesta

Vishal Panchal

Ranking: Expert

Mensajes: 279

Fecha de incorporación: 20 de mayo de 2012

Mensajes recientes

Hi Hitoshi Ozawa,

Thanks I was much explaining.
I Got it perfectly.


Thanks & Regards,
Vishal R. Panchal
Priyanka Dhingra
RE: javascript
2 de agosto de 2012 22:33
Respuesta

Priyanka Dhingra

Ranking: Liferay Master

Mensajes: 501

Fecha de incorporación: 19 de diciembre de 2011

Mensajes recientes

Thanks David and Hitoshi for sapring a minute to explain and enlightening upon a very basic but frequently usable concept.
emoticon
Priyanka Dhingra
RE: javascript
2 de agosto de 2012 23:27
Respuesta

Priyanka Dhingra

Ranking: Liferay Master

Mensajes: 501

Fecha de incorporación: 19 de diciembre de 2011

Mensajes recientes

David H Nebinger:
Priyanka Dhingra:
Hi John Nguyen,
You can keep the file in your portlet also
under the docroot->js folder
and use it as
1<script  type="text/javascript" src="/xxx-portlet/js/jquery.min.js"></script>


Wrong wrong wrong. If you have multiple copies of your portlet on a page, each copy is going to load the script. That is why you put it into the liferay-portlet.xml file, because Liferay will ensure only one copy of the script is loaded on the page, regardless of what portlets on the page are using it.


I have 5 portlets and all of them are using almost 5 js files. If I'll keep in theme will that be better than keeping in liferay-portlet.xml ??? keeping in theme does'nt make it reload every time and make it slow??
David H Nebinger
RE: javascript
3 de agosto de 2012 5:43
Respuesta

David H Nebinger

Ranking: Liferay Legend

Mensajes: 7512

Fecha de incorporación: 1 de septiembre de 2006

Mensajes recientes

Priyanka Dhingra:
I have 5 portlets and all of them are using almost 5 js files. If I'll keep in theme will that be better than keeping in liferay-portlet.xml ??? keeping in theme does'nt make it reload every time and make it slow??


You should put JS requirements into the theme only when they are site-wide requirements (because they will load on every page, regardless if one of your 5 portlets is on the page or not).

You could overcome this by creating a special theme for the page(s) the portlets are on, one that declares your normal theme as the parent in it's build.xml file. You'd have only one override in _diffs (to include the javascript files) plus the JS files. This has a maintenance impact though (when you make changes to the normal theme, you must build and deploy both themes (so the special theme will include the changes from normal theme)).

If you don't want to use the JS on all pages and you don't want to maintain a special theme, then putting the JS in liferay-portlet.xml is the way to go.
Jay Patel
RE: javascript
3 de agosto de 2012 10:11
Respuesta

Jay Patel

Ranking: Regular Member

Mensajes: 116

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

I know that keeping this in liferay-portlet.xml is always a good practice. But what I am not sure of is can we have multiple declaration of JS files in <header-portal-javascript>
OR <header-portlet-javascript> using comma or something. Does in latest version of Liferay, do they have this provision? Because often that it the case that one portlet may require multiple JS files & they may not required in the whole portlet & hence not required to keep in Theme.

-Jay.
David H Nebinger
RE: javascript
3 de agosto de 2012 11:19
Respuesta

David H Nebinger

Ranking: Liferay Legend

Mensajes: 7512

Fecha de incorporación: 1 de septiembre de 2006

Mensajes recientes

According to the code in PortletLocalServiceImpl._readLiferayPortletXML(), you use multiple <header-portlet-javascript /> tags in liferay-portlet.xml and they will each be used.

Same thing w/ the header-portal-javascript, header-portal-css, header-portlet-css and the associated footer-* entities.

Oops, I guess you can infer the same thing by actually reading the liferay-portlet-app DTD...
Priyanka Dhingra
RE: javascript
5 de agosto de 2012 22:43
Respuesta

Priyanka Dhingra

Ranking: Liferay Master

Mensajes: 501

Fecha de incorporación: 19 de diciembre de 2011

Mensajes recientes

Thanks David. emoticon
Hitoshi Ozawa
RE: javascript
6 de agosto de 2012 2:11
Respuesta

Hitoshi Ozawa

Ranking: Liferay Legend

Mensajes: 7954

Fecha de incorporación: 23 de marzo de 2010

Mensajes recientes

Example loading jquery:

<header-portlet-css>/css/main.css</header-portlet-css>
<header-portlet-css>/css/ui-lightness/jquery-ui-1.8.17.custom.css</header-portlet-css>
<header-portlet-javascript>/js/jquery-1.7.1.min.js</header-portlet-javascript>
<footer-portlet-javascript>/js/jquery-ui-1.8.17.custom.min.js</footer-portlet-javascript>
Jay Patel
RE: javascript
6 de agosto de 2012 7:00
Respuesta

Jay Patel

Ranking: Regular Member

Mensajes: 116

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

Thanks David, I had ignored DTD emoticon
Priyanka Dhingra
RE: javascript
22 de agosto de 2012 3:35
Respuesta

Priyanka Dhingra

Ranking: Liferay Master

Mensajes: 501

Fecha de incorporación: 19 de diciembre de 2011

Mensajes recientes

Hi,
I have tried to sum up everything in the following. hope It may be helpful to anyone facing the basic problems related to js files
http://michi-path.blogspot.in/2012/08/all-about-javascript-and-liferay.html