Welcome to the LimeSurvey Community Forum

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

Prevent accidentially closing the survey

More
9 years 7 months ago #111051 by LS_SR
Replied by LS_SR on topic Prevent accidentially closing the survey
Thanks for your fast answer.
The topic has been locked.
  • Taereian_43648
  • Taereian_43648's Avatar
  • Visitor
  • Visitor
9 years 2 months ago #116036 by Taereian_43648
Replied by Taereian_43648 on topic Prevent accidentially closing the survey
Thanks a lot for your Code.
Do you now how it's possible to make en exception for the languagechanger?

Cheers
Benny
The topic has been locked.
  • jonsen
  • jonsen's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
9 years 2 months ago #116038 by jonsen
Replied by jonsen on topic Prevent accidentially closing the survey
Hi fanepic,

please try to add ".changelang" to the jQuery "on" action by changing this:
Code:
$(document).on('click',".button,[name='move']", ...

to this:
Code:
$(document).on('click',".button,.changelang,[name='move']", ...

Please leave a quick comment if this works, otherwise I'll have a closer look.

Best
jonsen
The following user(s) said Thank You: Taereian_43648
The topic has been locked.
  • jonsen
  • jonsen's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
9 years 2 months ago #116039 by jonsen
The topic has been locked.
  • Taereian_43648
  • Taereian_43648's Avatar
  • Visitor
  • Visitor
9 years 2 months ago #116042 by Taereian_43648
Replied by Taereian_43648 on topic Prevent accidentially closing the survey
Thank you jonson works fine.

Do you also now how to make it Multilanguage like only show if it's German?

Regards
fanepic
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
9 years 2 months ago #116085 by Mazi
Replied by Mazi on topic Prevent accidentially closing the survey
You can try using the {LANG} placeholder and maybe put everything into an IF statement like
...
if({LANG}=='de')
{
...
}

{LANG} should get replaced by Limesurvey with the current survey language.

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
  • jonsen
  • jonsen's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 years 11 months ago - 8 years 11 months ago #118624 by jonsen
Replied by jonsen on topic Prevent accidentially closing the survey
Hi everyone,

the code has been working well on all of my past surveys. BUT now I have a survey which contains a file uploader question and there IT DOESN'T WORK CORRECTLY!

How to replicate the problem:
1. Add the code ( manual.limesurvey.org/Workarounds:_Surve...y_closing_the_survey ) to your template.js
2. Add a file uploader question
3. Navigate through the survey. If you open the file uploader, you will be asked if you want to leave the page EVEN THOUGH you use the LS navigation (the "next" or "previous" buttons).
4. Notice that you will not have the this problem if you just pass through that question without opening the uploader iframe.

The function works based on a <input type="hidden" id="submitted" /> tag being added when using the navigation, which then again is being checked for by the "goodbye" function to see if the buttons were used. The interesting point: Even though this tag is appeded to the end of the <body> correctly when using the navigation (even after opening the file uploader), the goodbye function does not recognize it. How can this be? It works flawlessly for all other pages, why not here? Does it have something to do with the iframe (which is the only differnce to other pages that I can see)?

I'm really looking forward to a new perspective on this, as I have been stuck on this for a week now. Thank you very much!

Best
Jonsen
Last edit: 8 years 11 months ago by jonsen.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 11 months ago #118659 by tpartner
Replied by tpartner on topic Prevent accidentially closing the survey
Yeah, I think the problem is that, with the introduction of a second <body> element in the iframe, the scope to look for the "#submitted" element gets messed up.

Additionally, I'm not keen on introducing unnecessary input elements - I think it's better handled by adding a class to the <body> element.

So, try this which uses a class instead of a hidden input to indicate a valid move and limits the scope for the <body> elements to the top window (ignoring anything in an iframe).

Code:
function goodbye(e) {
  if(!$('body', top.document).hasClass('submitted')) { //check if a button was used to navigate or the survey is completed
    if(!e) e = window.event;
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue = 'Are you sure you want to quit this survey?'; //This is displayed on the dialog for IE
 
    //e.stopPropagation works in Firefox.
    if (e.stopPropagation) {
      e.stopPropagation();
      e.preventDefault();
    }
 
    return 'Are you sure you want to quit this survey?'; // For chrome
  }
 
}
 
window.onbeforeunload = goodbye;
 
$(document).on('click', '.button, .changelang, [name="move"]', function(event){ //added .changelang here, please add other classes if you know which should be allowed to navigate
  $('body', top.document).addClass('submitted');
});

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: jonsen, Sweden
The topic has been locked.
  • jonsen
  • jonsen's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 years 11 months ago #118701 by jonsen
Replied by jonsen on topic Prevent accidentially closing the survey
Thank you, Tony! I re-added !$('#completed').length for the final page, so one can leave the finished survey without being asked and also tried to fix it for the template editor by looking for the '.templateeditor' class...

Hope this does it for now! I updated the workaround in the wiki ...
Code:
//FEATURES: (2015-03-27) Now with fix for iframes (e.g. file uploader) and the template editor 
function goodbye(e) {
    if(!$('body', top.document).hasClass('submitted') &amp;&amp; !$('#completed').length &amp;&amp; !$('.templateeditor', top.document).length) { //check if a button was used to navigate OR the survey is completed OR the template is displayed in the template editor
The topic has been locked.
More
4 years 3 months ago #191526 by blocka
Replied by blocka on topic Prevent accidentially closing the survey
Is there a solution to address this in LS 3.20.2+191119 ?

The JS in the wiki when dropped into the custom.js for a theme doesn't seem to have any affect....
The topic has been locked.
More
4 years 3 months ago #191527 by blocka
Replied by blocka on topic Prevent accidentially closing the survey
And then I found the answer. You can add this to custom.js:

(window).on('beforeunload', function(){
var c=confirm();
if(c){
return true;
}
else
return false;
});
The following user(s) said Thank You: Sweden
The topic has been locked.
More
4 years 1 week ago - 4 years 1 week ago #195219 by Sweden
Replied by Sweden on topic Prevent accidentially closing the survey
This is strange - I guess I have inserted the code mentioned here , and the code in the previous post, in Custom.js and then forgot all about it. However, the result is not what I hoped it would be.

I would like to see the warning in the end message (after the survey has been submitted), if someone tries to close the tab. It doesn't work that way now - the tab closes without any warning. How do I change that?

On the other hand, I get an unwanted warning when I try to save the results as PDF. I use the plugin PDFreport where the PDF is generated when you click on the "print answers" link (settings in PDFreport/file upload question is set to "Replace public print answer"). How can I prevent that warning and still keep the other (that isn't working yet)?

LS Version is 3.22.7+200225
Last edit: 4 years 1 week ago by Sweden.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose