passing doubles quotes in xml [duplicate] - javascript

This question already has answers here:
Escape double quote character in XML
(8 answers)
Closed 7 years ago.
I am working on a project where I am saving my data in xml format.I am unable to save double quotes in my data.I have tried replacing double quotes with " but it didn't worked.Please suggest how to achieve this.
Following is the xml data which I am passing
<question HasOptions="0" answer=""I think ki I am vulnerable"...text check // "i am afraid"" QuestionId="2042" OptionId="1260" ></question>
My javascript code for xml creation
function GetXMLForPosting() {
// here is the XML for posting
var xmlqQuestionResponse = '<question HasOptions="{{hasOptions}}" answer="{{score}}" QuestionId="{{questionID}}" OptionId="{{optionID}}" />';
var answers = '';
//lopping through questions
$('.little_text').each(function () {
// question data
hasOptions = $(this).attr('hasoptions');
questionID = $(this).attr('questionid');
// option data
selectedOption = $(this).find('input[name=radio_' + questionID + ']:checked');
score = $(selectedOption).attr('value');
optionID = $(selectedOption).attr('id');
// end
if (questionID > 0) {
if (hasOptions == 0) {
// case of textarea
// overiding default values
selectedOption = $(this).find('textarea[name=radio_' + questionID + ']');
score = $(selectedOption).val();
optionID = '1260';
answers = answers + xmlqQuestionResponse
.replace('{{hasOptions}}', hasOptions)
.replace('{{score}}', score.replace(/\'/g, "\'"))
.replace('{{questionID}}', questionID)
.replace('{{optionID}}', optionID);
}
else {
// normal radio button
answers = answers + xmlqQuestionResponse
.replace('{{hasOptions}}', hasOptions)
.replace('{{score}}', score)
.replace('{{questionID}}', questionID)
.replace('{{optionID}}', optionID);
}
}
});
//console.log('<questions>' + answers + '</questions>');
return '<questions>' + answers + '</questions>';
}
here my answer is coming in score option
My cs code
[HttpPost]
public ActionResult Postback(FormCollection formCollection)
{
// Process the take questionnaire
var model = (WorkflowMessage)Session[Enumerations.SessionItems.ViewModel];
// Check the page guid redirect accoringly
if (!GuidValid())
return ReshowPage();
var rawData = formCollection[Enumerations.FlashVariableFormPostKey.OutputData];
var enc = Encoding.GetEncoding("ISO-8859-1");
var responseXml = HttpUtility.UrlDecode(rawData, enc);
// redundant variable left so that at debug time the result can be inspected.
var result = new QuestionnaireService().SaveTakenQuestionnaire(PatientSessionDataObject.TreatmentId, model.PatientId, PatientSessionDataObject.CustomerId, model.AssetData, rawData);
return RedirectToAction("Next", "Treatment", new { navigationChosen = "Next", area = string.Empty, model.PageCheckGuid });
}
Please suggest how I can save text-"I think ki I am vulnerable"...text check // "i am afraid" in my answer field.Thanks

You need to use ".
For your example...
<question HasOptions="0" answer=""I think ki I am vulnerable"...text check // "i am afraid"" QuestionId="2042" OptionId="1260" ></question>

Related

javascript: how to save string substitution in another string? [duplicate]

This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 1 year ago.
I have a string and array
var s1 = "hello %s, i am %d years old";
var s2 =[John,24];
Expected result:
s3 = hello John i am 24 years old
I want to save the output into another string.
I'm able to display output in console
console.log(s1, ...s2)
But not able to store in other string.
I tried many things like:
var s3 = s1.format(...s2)
Any suggestions?
Unfortunately there is no string formatter available in JS, you'd have to write that manually like
let i = 0;
s3 = s1.replace(/%(s|d)/g, (_, type) => s2[i++]);
You could use the template string from ES6:
let anotherString = `Hello ${s2[0]}, I am ${s2[1]} years old`;
You could use this too:
String.prototype.format = function() {
a = this;
for (k in arguments) {
a = a.replace("{" + k + "}", arguments[k])
}
return a
}
Usage:
let anotherString = '{0} {1}!'.format('Hello', 'Word');
Than those solutions I dont see any other way to do that.

Why is my way of extracting newlines from textarea working with \r [duplicate]

This question already has an answer here:
carriage return in textarea retrieved as line feed
(1 answer)
Closed 3 years ago.
If I set the value of a textarea to let's say "123456\r123" and call document.getElementById('myTextarea').value.indexOf("\n"), it returns 6, which is the position of the line break; but why is it not returning -1, as there is no \n inside the string?
I guess the browser auto transfer the '\r' to '\n' when you set value to the textarea.
When I run those codes in browser,the result told me what I guess is just the truth.
let textArea = document.getElementById("inputTextarea");
let value = "abcd\radc";
textArea.value = value;
let out = document.getElementById("outputPre");
document.getElementById("inputTextarea").addEventListener("click", () => {
value = textArea.value;
let chars = value.split("").map(s => s.charCodeAt(0));
let lf = ["\r".charCodeAt(0), "\n".charCodeAt(0)];
out.innerHTML = 'valueChars:<em>' + value.split("") + "</em><br/>ASCII:<em>" + chars.toString() + "</em>";
/*valueChars:a,b,c,d,
,a,d,c ASCII:97,98,99,100,10,97,100,99*/
console.info(lf); //13,10
});

IE not liking URL variable? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
Hey guys, I have this quite simple script which works great on chrome & firefox.
The idea is to nab "choice" value from the URL and proceed with the script. As mentioned other browsers work fine, but IE seems to have trouble with it.
Does anyone know a workaround? I don't want to just run the function on the previous page.
var url1 = "https://www.youtube.com";
var url2 = "http://ign.com";
function jukebox()
{
let params = (new URL(document.location)).searchParams;
let choice = params.get("choice");
if ( choice == 1 )
{
window.location=(url1);
}
else if ( choice == 2 )
{
window.location=(url2);
}
}
jukebox();
redirect();
IE does not support searchParams but you could try writing your own parse function:
var params = {};
var str = document.location;
var start = str.indexOf("?");
document.write(start+"<br>")
start += str.slice(start).indexOf("choice=") + 7;
var end = start + str.slice(start).indexOf("&") + 1;
if (!end) end = str.length
var choice = str.slice(start, end);

How do I use Jquery variable variables? [duplicate]

This question already has answers here:
Access Javascript variables dynamically
(4 answers)
Closed 8 years ago.
I know there are a lot of questions about if it is possible to use variable variables in jQuery.
One of the questions is this one: click here.
I tried to use the answer, but I don't know how I can use it in my case.
var numberofquestions = 10;
var dataString = "";
for ( var i=1; i<=numberofquestions; i++ ) {
/* ------ first part ------- */
if (i==1) {
dataString = dataString + "q1=" + question1 + "&";
} /* ------ end first part ------- */
else if (i == numberofquestions) {
questionValue = "question" + numberofquestions;
qValue = "q" + numberofquestions;
dataString = dataString + qValue + "=" + questionValue;
console.log(dataString);
} else {
questionValue = question + i;
dataString = dataString + "q" + i + "=" + questionValue + "&";
}
}
The loop will run 10 times, and each time it needs to add a part to the already existing dataString.
What it needs to do is make this string:
q1=(value of var question1)&q2=(value of var question2) and so forth.
The vars question1, question2, ... question10 all hold a number.
The first part works, it outputs q1=5 in the console log, however, after comes a random string. The output string (the total string) looks like:
q1=5&q2=NaN&q3=NaN&q4=NaN&q5=NaN&q6=NaN&q7=NaN&q8=NaN&q9=NaN&q10=question10
Does anybody know what I'm doing wrong?
You should use an array for this. There is no such thing as "variable variables" in JavaScript.
You can access a variable through a string containing the variables name by using this[variableName], but again, you shouldn't. You should use an array for this.
In your case, you would use questionValue = this["question" + i], but one more time: Don't do it. Use an array instead.
I'm not sure why you're using "question" + numberofquestions which will be 10 every time

getting parameter from url [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Get escaped URL parameter
(19 answers)
Closed 9 years ago.
I have this url index.html#secondPage?name=the%20second%20page
I want to get the value of name ("the second page") using javascript and jquery
thanks
You can use the code from the answers to this question. The only difference is that you want to parse the location.hash instead of location.search so just change that in whichever answer you choose to go with.
You'll also need to use substr to delete the leading # like:
var hash = window.location.hash.substr(1);
Here is the code from my answer to the question I linked to, with the modification:
function get_query(){
var url = location.hash.substr(1);
var qs = url.substring(url.indexOf('?') + 1).split('&');
for(var i = 0, result = {}; i < qs.length; i++){
qs[i] = qs[i].split('=');
result[qs[i][0]] = decodeURIComponent(qs[i][1]);
}
return result;
}
You can use it like:
var secondPage = get_query()['name']; // 'the second page'
Try like below, It will help you
Fiddle : http://jsfiddle.net/RYh7U/144/
Javascript :
function GetURLValue (sKey) {
return unescape("index.html#secondPage?name=the%20second%20page".replace(new RegExp("^(?:.*[&\\?]" + escape(sKey).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}
alert(GetURLValue("name"));

Categories