Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Adding custom javascript file

More
10 years 2 months ago #104125 by rvas
Adding custom javascript file was created by rvas
Hi all,

I would like to add auto-hyphenation to matrix questions so that the text stays in the right place when displayed on small resolution devices such as older mobile phones. I am thinking of using Hypher ( github.com/bramstein/hypher ), but I need to include in the page-headers where matrix questions are to be displayed. Do you have any ideas what would be the least intrusive way to implement this, without risking of breaking the survey?

Also, I have added custom made javascript code in several questions that seem to work fine. I haven't disabled XSS, contrary to what the manual suggests; do you think there will there be any unpredicted problems if I leave XSS on while using custom JS inside some questions?

Thanks in advance!
The following user(s) said Thank You: Ben_V
The topic has been locked.
More
10 years 2 months ago #104126 by Ben_V
Replied by Ben_V on topic Adding custom javascript file
Hi,

1) inside templates files:
the better place is probably inside the startpage.pstpl file of your template or endpage.psptl if all functions are working correctly.

An option is to place your link inside question.pstpl file to avoid unnecesary extra resource download in all pages (welcome, completed, assessment etc. )

2) inside the survey
If it's only for matrix question, another approach is to paste your link into the source of the group description, when this group contains a matrix question. The advantage is that if you change the template, you don't have to remember this adaptation... ( obviously your hypher.js file have to belong to the application script folder and not to the template)
.

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The following user(s) said Thank You: rvas
The topic has been locked.
More
10 years 2 months ago #104128 by rvas
Replied by rvas on topic Adding custom javascript file
Thank you for your very quick response!

I faced problems trying to put the code in the question group description, so I finally decided to place a modified code in template.js inside the ready function and detect via jquery if <th> tag is present before dynamically loading the necessary hyphen js files. It seems to work perfect. Below is the actual code in case anyone finds this useful:
Code:
$(document).ready(function(){
  if ($('th').length){
    $.getScript( "/survey/scripts/jquery.hypher.js" )
      .done(function() {
        $.getScript( "/survey/scripts/en-us.js" )
          .done(function() {
            $('th').hyphenate('en-us');
          })
      })
  }
})

A last question, what is the correct tag to point to the scripts folder instead of hard-coding it? For example, I know that the active template folder is displayed using {TEMPLATEURL}, but what about scripts url?
The topic has been locked.
More
10 years 2 months ago - 10 years 2 months ago #104130 by Ben_V
Replied by Ben_V on topic Adding custom javascript file
Thanks for sharing your script; it sounds like a good solution ;)

For your last question, I think that this placeholder does not exist...

Note:
1) Some {KEYWORDS} are working only if placed in particulary files (see details in the doc: Roadmap )
2) An existing Jquery folder is available in the scripts/folder. It's the proper place for jquery plugins.
3) there is no problem in hard-coding a custom template (upload/templates) because they won't be affected when updating the software.

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last edit: 10 years 2 months ago by Ben_V.
The following user(s) said Thank You: rvas
The topic has been locked.
More
10 years 2 months ago #104132 by rvas
Replied by rvas on topic Adding custom javascript file
I'll leave it as is then, many thanks!

Regarding the XSS protection, is this option only supposed to protect against malicious survey operators or if disabled leaves the survey unprotected against respondents who can place malicious code inside comments and text forms? In my case, I use JS in several questions, but everything seem to work regardless whether this option is on or off. My survey will be open to the web so I want to make sure that I have taken the maximum precautions.
The topic has been locked.
More
10 years 2 months ago #104133 by Ben_V
Replied by Ben_V on topic Adding custom javascript file
Good question...I'm not sure if it changes seriously the security level of text questions. May someone else answer this point.

I personnally used to:

1) Use captcha to take the survey; (I experienced some problems before, but nothing since I use it: my surveys stop to be strangely popular in Nigeria or Philippines :unsure: )

2) Set restrictive validation for all text fields (max. number of characters)

3) Install LS in a subdomain with dedicated robots.txt and .htaccess files (Apache server); If you can, it's better to avoid web-crawlers indexation.

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 2 months ago #104149 by DenisChenu
Replied by DenisChenu on topic Adding custom javascript file
Hi,

If it's only for modern browser, why not use something like that:
Code:
-webkit-hyphens: auto;
   -moz-hyphens: auto;
        hyphens: auto;
www.w3.org/TR/2011/WD-css3-text-20110412/#hyphens ?

Denis

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose