Foros de discusión
[RESOLVED] Can We Include a JavaScript File Dynamically from a Portlet?
Mike Harris, modificado hace 12 años.
[RESOLVED] Can We Include a JavaScript File Dynamically from a Portlet?
Junior Member Mensajes: 91 Fecha de incorporación: 28/03/11 Mensajes recientes
I know that we can include a JavaScript inside liferay-portlet.xml with the header-portlet-javascript tag but I need to include a different JavaScript based on the language.
For example : http://www.externalsite.com/data.js?lang=en for english and http://www.externalsite.com/data.js?lang=fr for french, etc...
I can't just include each javascript for all the languages for all the pages because the script initialize variables based on the language, but always uses the same variable names.
Note : It's for Liferay 6.1
For example : http://www.externalsite.com/data.js?lang=en for english and http://www.externalsite.com/data.js?lang=fr for french, etc...
I can't just include each javascript for all the languages for all the pages because the script initialize variables based on the language, but always uses the same variable names.
Note : It's for Liferay 6.1
jelmer kuperus, modificado hace 12 años.
RE: Can We Include a JavaScript File Dynamically from a Portlet?
Liferay Legend Mensajes: 1191 Fecha de incorporación: 10/03/10 Mensajes recientes
Mike Harris, modificado hace 12 años.
RE: Can We Include a JavaScript File Dynamically from a Portlet?
Junior Member Mensajes: 91 Fecha de incorporación: 28/03/11 Mensajes recientes
I tried that but Liferay seems to think that the element has a body and render something like :
<script href="http://externalsite.com/script.js?language=en" type="text/javascript"/>/*<![CDATA[*/
<link class="lfr-css-file" href="/theme/css/main.css?browserId=other&....
<style type="text/css">
</style>
<script type="text/javascript" src="/transcontinental-corpo-theme/js/jquery-1.7.2.min.js">/*]]>*/</script>
etc...
Here's the code I used :
<script href="http://externalsite.com/script.js?language=en" type="text/javascript"/>/*<![CDATA[*/
<link class="lfr-css-file" href="/theme/css/main.css?browserId=other&....
<style type="text/css">
</style>
<script type="text/javascript" src="/transcontinental-corpo-theme/js/jquery-1.7.2.min.js">/*]]>*/</script>
etc...
Here's the code I used :
org.w3c.dom.Element jsLink = renderResponse.createElement("script");
jsLink.setAttribute("type", "text/javascript");
jsLink.setAttribute("src", "http:/externalsite.com/script.js?language=" + lang");
renderResponse.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, jsLink);
jelmer kuperus, modificado hace 12 años.
RE: Can We Include a JavaScript File Dynamically from a Portlet?
Liferay Legend Mensajes: 1191 Fecha de incorporación: 10/03/10 Mensajes recientes
Sounds like a bug to me. Maybe you could try
to work around it
jsLink.setTextContent(" ");
to work around it
Mike Harris, modificado hace 12 años.
[RESOLVED] RE: Can We Include a JavaScript File Dynamically from a Portlet?
Junior Member Mensajes: 91 Fecha de incorporación: 28/03/11 Mensajes recientes
Mmm. this worked. Thanks for the tip, I'm going to file a Jira bug.
Here's how I did it... That's a method inside my portlet Action class :
In portlet.xml, you need to add this :
Here's how I did it... That's a method inside my portlet Action class :
@Override
protected void doHeaders(RenderRequest renderRequest, RenderResponse renderResponse) {
super.doHeaders(renderRequest, renderResponse);
String lang = "en";
lang = lang.toLowerCase();
org.w3c.dom.Element jsLink = renderResponse.createElement("script");
jsLink.setAttribute("type", "text/javascript");
jsLink.setAttribute("src", "http://www.mywebsite.com?language=" + lang + "&autoload=true");
jsLink.setTextContent(" "); // important
renderResponse.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, jsLink);
}
In portlet.xml, you need to add this :
<container-runtime-option>
<name>javax.portlet.renderHeaders</name>
<value>true</value>
</container-runtime-option>