Use the value of "End URL" in JavaScript etc.

More
5 years 4 months ago #72245 by Marten
Marten created the topic: Use the value of "End URL" in JavaScript etc.
Hi,

If I have e.g. www.domain.com in the "End URL" field for my survey, but want to exit to that URL halfway through the survey, by using javascript.

Instead of hard coding the url in the javascript it would be nice to just use the value of "End URL", so I don't have to change in multiple places if changing the end url.

Using version 1.91+ Build 11379.

Thank you for your input.

Please Log in to join the conversation.

More
5 years 4 months ago #72248 by Mazi
Mazi replied the topic: Use the value of "End URL" in JavaScript etc.
According to docs.limesurvey.org/The+template+editor&...+LimeSurvey#Keywords you schould be able to use this placeholder if you add the JS at your template: "{url}"


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
5 years 4 months ago #72252 by Marten
Marten replied the topic: Use the value of "End URL" in JavaScript etc.
Thanks, Mazi. Will try that!

I knew I'd seen that somewhere, but were unable to find it when searching (searched for End URL and end_url).

/Marten

Please Log in to join the conversation.

More
5 years 4 months ago #72254 by Mazi
Mazi replied the topic: Use the value of "End URL" in JavaScript etc.
Once you found a solution it would be nice if you could add some explanation here.

Thanks!


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
5 years 4 months ago #72283 by Marten
Marten replied the topic: Use the value of "End URL" in JavaScript etc.
Absolutely!

The {URL} (seems like it must be uppercase, but in wiki it's in lowercase) inserts as
<a href='www.domain.com'>My Domain</a>
so need to strip it.

I put the following at the top of "question.pstpl" for my template:
<script type="text/javascript" charset="utf-8">
	function gotoEndURL(){
		var sURL = "{URL}";
		sURL = sURL.substring(0, sURL.indexOf(">"));
		sURL = sURL.substring((sURL.indexOf("'") + 1), sURL.lastIndexOf("'"));
		window.location = sURL;
	}
</script>

For my specific case I want to exit based on a quite complex set of conditions, so made a specific question first in the next group (where I wanted the exit to happen, since I wanted the current aopge to be stored before exiting - this could probably be solved better) for this with the following javascript:
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() { 
		if($("#question1128").css("display") && $("#question1128").css("display") != "none"){
			$("#question1128").hide();
			gotoEndURL();
		}
	});
</script>

The question is only shown if the conditions eval to true and if the question is shown the JavaScript if statement executes to true (display != "none"). The first check of the if statement was to stop the JS being executed when going into the quick translation page (where I first got thrown out when I had the full window.location hard coded in the question - guess this could be removed as the gotoEndURL function is unavailable in the translation pages).

The only thing now is that you see the page for the next group (where the "exit question" is) render before being redirected.

This works for me for now. Possible improvements could be putting the
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() { 
		if($("#question1128").css("display") && $("#question1128").css("display") != "none"){
			$("#question1128").hide();
			gotoEndURL();
		}
	});
</script>

as a listener for the question show/hide instead (that way you could have the "exit question" in the same group as the questions triggering it) and/or doing a post for the answers on the current page before executing the window.location, getting rid of the need to go to the next group.

But I kinda like that the user needs to press "Next" before the JS is executed, so they don't get thrown out just for clicking the wrong field on a question where they otherwise would have changed their answer before submitting.

/Marten

Please Log in to join the conversation.

More
5 years 4 months ago #72290 by Mazi
Mazi replied the topic: Use the value of "End URL" in JavaScript etc.
Great solution. Can you please add your solution to the workaround section of our manual so other users can beneffit from your solution, too!?

Thanks!


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
5 years 4 months ago #72323 by tpartner
tpartner replied the topic: Use the value of "End URL" in JavaScript etc.
Marten, this may get rid of the question flashing before the redirect:
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() { 
		if($('#question1128').is(':visible')){
			$('body').hide();
			gotoEndURL();
		}
	});
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 4 months ago #72326 by Marten
Marten replied the topic: Use the value of "End URL" in JavaScript etc.

tpartner wrote: Marten, this may get rid of the question flashing before the redirect:

<script type="text/javascript" charset="utf-8">
	$(document).ready(function() { 
		if($('#question1128').is(':visible')){
			$('body').hide();
			gotoEndURL();
		}
	});
</script>


Thanks, will probably solve it!

Please Log in to join the conversation.

More
5 years 4 months ago - 5 years 4 months ago #72329 by Marten
Marten replied the topic: Use the value of "End URL" in JavaScript etc.

tpartner wrote: Marten, this may get rid of the question flashing before the redirect...


I simplified it a bit by putting the body hide in the exit function instead, since this should happen whenever that function is called.
<script type="text/javascript" charset="utf-8">
	function gotoEndURL(q){
		if($("#" + q).css("display") && $("#" + q).css("display") != "none"){
			$('body').hide();
			var sURL = "{URL}";
			sURL = sURL.substring(0, sURL.indexOf(">"));
			sURL = sURL.substring((sURL.indexOf("'") + 1), sURL.lastIndexOf("'"));
			window.location = sURL;
		}
	}
</script>

Hence the calling function was simplified to
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() { 
		gotoEndURL("question1128");
	});
</script>

/Marten
Last Edit: 5 years 4 months ago by Marten.

Please Log in to join the conversation.

More
5 years 4 months ago #72332 by tpartner
tpartner replied the topic: Use the value of "End URL" in JavaScript etc.
Yup, and if you have a dedicated template, you can place all of the code in template.js for ease of maintenance.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now