How do I change the page requesting a token for authentication? (copied)

More
3 months 1 day ago - 3 months 1 day ago #172461 by tpartner
tpartner replied the topic: How do I change the page requesting a token for authentication? (copied)
A couple of things...

1) You cannot access Expression Manager variables from template.js - they are only available in the survey. Try this to access the SID:

	// Find the survey and group IDs
	if($( 'input#fieldnames' ).length != 0) {
		var fieldNames = $('input#fieldnames').attr('value');
		var tmp = fieldNames.split('X');
		var sID = tmp[0];
		var gID = tmp[1];
	}


2) Using the triple-equals sign searches for an exact comparison, including variable type, and since you have enclosed {SID} in quotes, it will be a string and then you try to compare it to a number in your IF() statement. Try this (given the var from above):

if (sID == 111111){

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 3 months 1 day ago by tpartner.
The following user(s) said Thank You: socius

Please Log in or Create an account to join the conversation.

More
3 months 12 hours ago - 3 months 12 hours ago #172506 by socius
socius replied the topic: How do I change the page requesting a token for authentication? (copied)
Hi,

thanks so much @tpartner!

I tried to get the SID with the given code, but it did not return the SID, since console.log($('input#fieldnames' ).length); . returns 0, i.e. there are no fieldnames on the page where participants can enter their token. Right?
$( document ).ready( function() {
	// Find the survey and group IDs
	if($( 'input#fieldnames' ).length != 0) {
		var fieldNames = $('input#fieldnames').attr('value');
		var tmp = fieldNames.split('X');
		var sID = tmp[0];
		var gID = tmp[1];
	}
 
	// But: this does not work on this page, because ...
	console.log($('input#fieldnames' ).length);  // ... returns 0, i.e. there are no fieldnames on the page where participants can enter their token. Right?
});

But that brought me to the idea to extract the SID from the URL - unfortunately it's hardcoded, but it works in my case: I get the survey specific token messages I wanted.
$(document).ready(function(){
 
	/* Extract SID from URL */
	var pathname = window.location.pathname; /* Get the pathname from the URL */
	var pathArray = window.location.pathname.split( '/' );
	var sID = pathArray[3]; /* in my case (LSdirectory/index.php/111111/newtest/Y#) the SID sits at position 3 */
	console.log(sID);  /* returns the SID extracted from the URL */
 
	if (sID == 111111){
 
		if (typeof tokenlabel === "undefined"){
        tokenlabel ="1 YOUR TOKEN LABEL : ";
		}
 
		if (typeof tokentext === "undefined"){
        tokentext ="1 YOUR TOKEN TEXT DESCRIPTION {SID}";
		}
 
		if (typeof tokenerror === "undefined"){
        tokenerror ="1 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
		}
	}
 
	if (sID != 111111){
 
		document.getElementById("token").style.display="none";  /* hides the tokenform */
 
		if (typeof tokenlabel === "undefined"){
        tokenlabel ="2 YOUR TOKEN LABEL : ";
		}
 
		if (typeof tokentext === "undefined"){
        tokentext ="2 YOUR TOKEN TEXT DESCRIPTION {SID}";
		}
 
		if (typeof tokenerror === "undefined"){
        tokenerror ="2 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
		}
 
	}
 
   if ($("#tokenform").length )
   {
        $("#tokenmessage").html(tokentext);
        $("label[for='token']").text(tokenlabel);
   }
   else
   {
       $("#tokenmessage").html(tokentext+tokenerror);
   }
 
})

Also thanks a lot for your hint with the "===" exact comparison - it's decisive in this case - thanks a lot!

Is the upper code "OK"? Or is there something I should avoid? (probably hardcoding :-)


Thanks and all the best,
G
Last Edit: 3 months 12 hours ago by socius. Reason: Add question

Please Log in or Create an account to join the conversation.

More
3 months 11 hours ago - 3 months 11 hours ago #172508 by tpartner
tpartner replied the topic: How do I change the page requesting a token for authentication? (copied)
Oh, yeah, sorry I forgot that you weren't in the survey yet so there is no fieldnames input.

Parsing the URL works but you could get the sID from the action attribute of the form:



var sID = $('#limesurvey').attr('action').split('sid=')[1];

Regarding your code, it may be slightly more efficient to use an ELSE statement instead of two IFs when testing for the sID.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last Edit: 3 months 11 hours ago by tpartner.
The following user(s) said Thank You: socius

Please Log in or Create an account to join the conversation.

More
3 months 10 hours ago #172524 by socius
socius replied the topic: How do I change the page requesting a token for authentication? (copied)
In version 2.6.7LTS I see
</script><form id="tokenform" autocomplete="off" action="/fragen/index.php/111111" method="post">
If that's correct I simply stay with the extraction from the URL.

I replace the second
if (sID != 111111){
by
else {
which works! Thanks again!

Best,
G

Please Log in or Create an account to join the conversation.

More
2 months 3 weeks ago #172717 by socius
socius replied the topic: How do I change the page requesting a token for authentication? (copied)
Hi,

Just for the record: I found another solution to get the sid which is a little less hardcoded (it does not fix the position, at least). I had to look for such a solution since I just ran into an error caused by this ;-)

Best, G

$(document).ready(function(){
 
	/* Extract SID from URL */
	var pathname = window.location.pathname; /* Get the pathname from the URL */
	var pathArrayStep1 = window.location.pathname.split( 'sid/' )[1];  /* Split the pathname in two (at the sid) and get the second (=1) element of the array */
	var sID = pathArrayStep1.split( '/' )[0];  /* Split the pathname at the "/" and get the first (=0) element of the array */
	console.log(sID);  /* returns the SID extracted from the URL */
 
 
	if (sID == 111111){
 
		if (typeof tokenlabel === "undefined"){
        tokenlabel ="1 YOUR TOKEN LABEL : ";
		}
 
		if (typeof tokentext === "undefined"){
        tokentext ="1 YOUR TOKEN TEXT DESCRIPTION {SID}";
		}
 
		if (typeof tokenerror === "undefined"){
        tokenerror ="1 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
		}
	}
 
	if (sID != 111111){
 
		document.getElementById("token").style.display="none";  /* hides the tokenform */
 
		if (typeof tokenlabel === "undefined"){
        tokenlabel ="2 YOUR TOKEN LABEL : ";
		}
 
		if (typeof tokentext === "undefined"){
        tokentext ="2 YOUR TOKEN TEXT DESCRIPTION {SID}";
		}
 
		if (typeof tokenerror === "undefined"){
        tokenerror ="2 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
		}
 
	}
 
   if ($("#tokenform").length )
   {
        $("#tokenmessage").html(tokentext);
        $("label[for='token']").text(tokenlabel);
   }
   else
   {
       $("#tokenmessage").html(tokentext+tokenerror);
   }
 
})
 

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!