Welcome, Guest
Username: Password: Remember me

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

Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72245

  • Marten
  • Marten's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 12
  • Karma: 0
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.
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72248

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5426
  • Thank you received: 307
  • Karma: 252
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)limesurvey.org'"
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72252

  • Marten
  • Marten's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 12
  • Karma: 0
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
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72254

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5426
  • Thank you received: 307
  • Karma: 252
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)limesurvey.org'"
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72283

  • Marten
  • Marten's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 12
  • Karma: 0
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
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72290

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5426
  • Thank you received: 307
  • Karma: 252
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)limesurvey.org'"
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72323

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4372
  • Thank you received: 830
  • Karma: 381
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72326

  • Marten
  • Marten's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 12
  • Karma: 0
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!
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72329

  • Marten
  • Marten's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 12
  • Karma: 0
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: 2 years 10 months ago by Marten.
The administrator has disabled public write access.

Re: Use the value of "End URL" in JavaScript etc. 2 years 10 months ago #72332

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4372
  • Thank you received: 830
  • Karma: 381
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.151 seconds
Donation Image