Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Mike Harris
[RESOLVED] Can We Include a JavaScript File Dynamically from a Portlet?
April 4, 2012 7:53 AM
Answer

Mike Harris

Rank: Junior Member

Posts: 91

Join Date: March 28, 2011

Recent Posts

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
Jelmer Kuperus
RE: Can We Include a JavaScript File Dynamically from a Portlet?
March 29, 2012 9:41 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

I have never done it but jsr286 allows it
Mike Harris
RE: Can We Include a JavaScript File Dynamically from a Portlet?
March 29, 2012 10:00 AM
Answer

Mike Harris

Rank: Junior Member

Posts: 91

Join Date: March 28, 2011

Recent Posts

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&amp....
<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 :

1       org.w3c.dom.Element jsLink = renderResponse.createElement("script");
2        jsLink.setAttribute("type", "text/javascript");
3        jsLink.setAttribute("src", "http:/externalsite.com/script.js?language=" + lang");
4        renderResponse.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, jsLink);
Jelmer Kuperus
RE: Can We Include a JavaScript File Dynamically from a Portlet?
March 29, 2012 10:21 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

Sounds like a bug to me. Maybe you could try

1jsLink.setTextContent(" ");


to work around it
Mike Harris
[RESOLVED] RE: Can We Include a JavaScript File Dynamically from a Portlet?
May 22, 2012 11:12 AM
Answer

Mike Harris

Rank: Junior Member

Posts: 91

Join Date: March 28, 2011

Recent Posts

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 :

 1
 2    @Override
 3    protected void doHeaders(RenderRequest renderRequest, RenderResponse renderResponse) {
 4        super.doHeaders(renderRequest, renderResponse);
 5       
 6        String lang = "en";
 7        lang = lang.toLowerCase();
 8
 9         org.w3c.dom.Element jsLink = renderResponse.createElement("script");
10        jsLink.setAttribute("type", "text/javascript");
11        jsLink.setAttribute("src", "http://www.mywebsite.com?language=" + lang + "&autoload=true");
12        jsLink.setTextContent(" "); // important
13        renderResponse.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, jsLink);
14    }


In portlet.xml, you need to add this :

1        <container-runtime-option>
2            <name>javax.portlet.renderHeaders</name>
3            <value>true</value>
4        </container-runtime-option>