Foros de discusión
Adding Javascript to every page
Adding Javascript to every page
Regular Member Mensajes: 138 Fecha de incorporación: 24/1/12 Mensajes recientesI'm using Liferay EE version 6.1.10.
I've been searching for a way to add my own custom Javascript (also include for external JS files) to every page in the portal. I want to have a way to add JS that will affect all pages, whenever the need arises.
I tried to follow this tutorial http://www.liferay.com/web/samuel.kong/blog/-/blogs/adding-a-javascript-to-every-page and though it looked promising it didn't work for me unless I changed use-default-template to true in liferay-portlet.xml and manually added the portlet to the page. The trick there with setting layout.static.portlets.all in the properties file seemed to have no effect for me.
Any help on how to accomplish this would be greatly appreciated, thanks!
RE: Adding Javascript to every page
Regular Member Mensajes: 234 Fecha de incorporación: 22/10/11 Mensajes recientesAdd your java script code to init.jsp which is reside at /html/portal/init.jsp.
It will reflect on every jsp of the portal.
RE: Adding Javascript to every page
Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/3/10 Mensajes recientesAdd your java script code to init.jsp which is reside at /html/portal/init.jsp.
It will reflect on every jsp of the portal.
This will require a hook. Probably easier to use a theme here in case there's another hook.
RE: Adding Javascript to every page
Regular Member Mensajes: 138 Fecha de incorporación: 24/1/12 Mensajes recientesAdd your java script code to init.jsp which is reside at /html/portal/init.jsp.
It will reflect on every jsp of the portal.
This will require a hook. Probably easier to use a theme here in case there's another hook.
Hi, can you explain about the required hook? I don't want to place logic in my theme.
Thanks
RE: Adding Javascript to every page
Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/3/10 Mensajes recientesRE: Adding Javascript to every page
Liferay Master Mensajes: 550 Fecha de incorporación: 29/12/10 Mensajes recientesAdd that JavaScript file in theme(portal_normal.vm).
Hope it helps.
Thanks & Regards,
Amit Doshi
RE: Adding Javascript to every page
Regular Member Mensajes: 138 Fecha de incorporación: 24/1/12 Mensajes recientesPuneet - your idea definitely works, which is great but I see that the JS code is executed several times when the page loads. I added a
<script src="my_javascript_file.js" type="text/javascript"></script>
at the top of init.jsp and I see that it gets included like 10 times (when viewing the loaded scripts in the browser). Why is that?Amit, I am trying not to place logic in my theme since themes can change.
RE: Adding Javascript to every page (Respuesta)
Liferay Master Mensajes: 550 Fecha de incorporación: 29/12/10 Mensajes recientesI also have the same requirement as you have.
So for that reason i made one hook and we have changed the top_js-ext.jspf file.
Here I am specifying with the code of liferay-hook.xml and also the code in the top_js.ext.jspf
In liferay-hook.xml
<!--?xml version="1.0"?-->
<hook>
<custom-jsp-dir>/WEB-INF/jsp</custom-jsp-dir>
</hook>
In jsp make the folder structure as html/common/themes/top_js-ext.jspf same as we have inside ROOT Folder
and then written the below code to include js file in it.
<script type="text/javascript" src="/html/js/jquery/jquery-1.7.1.min.js"></script>
It will add js file only one time, not 10 times. Please check it and let me know.
Thanks & Regards,
Amit Doshi
RE: Adding Javascript to every page
Regular Member Mensajes: 138 Fecha de incorporación: 24/1/12 Mensajes recientesThat worked for me

RE: Adding Javascript to every page
New Member Mensajes: 11 Fecha de incorporación: 17/8/18 Mensajes recientesHi, Im looking for help for adding js libraries on multiple pages. Specifically moment.js. Have read multiple forum posts and the official guide for my Liferay version(7.0ga7) but none of them have been of any help. please guide me on the current practical way of adding js libraries across multiple pages please.
RE: Adding Javascript to every page
Regular Member Mensajes: 119 Fecha de incorporación: 1/10/13 Mensajes recientesHi Eduardo:
As of 7.1, we have included a new functionality that allows declaring resources to be included in every page in your bnd.bnd file. You can see an example of its use in this file -> https://github.com/liferay/liferay-portal/blob/master/modules/apps/frontend-js/frontend-js-web/bnd.bnd
Basically you have two headers: Liferay-JS-Resources-Top-Head-Authenticated and Liferay-JS-Resources-Top-Head. The first one included the latter so, if you put a resource in Liferay-JS-Resources-Top-Head, you don't need to relist it in Liferay-JS-Resources-Top-Head-Authenticated.
The paths are relative to the web context path of the module, so just place your JS files inside the META-INF/resources folder of your JAR and write paths starting at that folder.
Hope this helps,
Ivan