Escape special characters in javascript - javascript

$.ajax({
url: "/Search/GetMatchCount?idx=" + idx + "&q=" + escape(q) + "&filter=" + escape(filter) + "&fields=" + fields,
success: function (data) {
$("#tabprograms").html("Customer Programs (" + data + ")");
}
});
I have a string "civic+governance" in my search text-box.
I want to retrieve the value of the text-box in some local variable with jquery/javascript. My code is,
var q = document.getElementById('q').value;
The value that I get for q is "civic governance".
The '+' sign seems to be encoded with " ".
How do I not make it encoded to " " and have my string as it is , i.e. "civic+governance".

Your problem is that you are using ESCAPE(q). ESCAPE can not encode the + character you need to use the encodeURIComponent(q) then decode in php with rawurldecode() or you could also change your method to POST.

Related

how do I add two values to option value and seperate with with a comma javascript

I am trying to put two values in option value separating with comma using javascript but when I get the value from server it shows only the first value i.e course code
javascript Code
options += "<option value="+element.course_code+","+element.course_id+">(" + element.course_code + ") " + element.name + "</options>";
data.append('course_id', course_id);
Server
$myvalue = $request->get('course_id')
You should put quotes around the value so it is treated as one whole string
options += "<option value='"+element.course_code+","+element.course_id+"'>(" + element.course_code + ") " + element.name + "</options>";
You can try using the following code instead by declaring the single string.
The code should look like:
options += "<option value="+element.course_code+','+element.course_id+">(" + element.course_code + ") " + element.name + "</options>";

I am unable to get environment names from an array

I have an array in which the environment list is stored and from this list, I have to get all the environment names which I am unable to get right now. See below what I have tried and please help me out of this.
<script>
//${environmentList} is getting from backend i.e from modal class in which toString() method has stored with all its contents.
var envLength = `${environmentList}`.length;
var allEnv = `${environmentList}`;
console.log("All Environments : " + allEnv);
console.log("Length of environments : " + envLength);
for(let i = 0 ; i < envLength ; i++){
let all = `${allEnv[i].environmentName}`;
console.log("Environment Name : " + all[i]);
}
</script>
toString() method :
#Override
public String toString() {
return "EnvironmentModel [environmentId=" + environmentId + ", environmentName=" + environmentName
+ ", environmentDesc=" + environmentDesc + ", hostAddress=" + hostAddress + ", environmentURL="
+ environmentURL + ", configFileName=" + configFileName + ", configFilePath=" + configFilePath
+ ", companyId=" + companyId + ", createdBy=" + createdBy + ", createdAt=" + createdAt
+ ", lastUpdatedBy=" + lastUpdatedBy + ", lastUpdatedAt=" + lastUpdatedAt + ", elementFileName="
+ elementFileName + ", elementFilePath=" + elementFilePath + ", envStatus=" + envStatus + ", projectId="
+ projectId + ", project=" + project + "]";
}
OUTPUT:
All Environments :
[EnvironmentModel [environmentId=345, environmentName=MM_Env_1, environmentDesc=Environment_1, hostAddress=null, environmentURL=null, configFileName=SystemFile.xml, configFilePath=/env_files/MM_Env_1, companyId=1, createdBy=2, createdAt=2019-12-18 16:39:38.595, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:42:13.259, elementFileName=ElementFile.xml, elementFilePath=/env_files/MM_Env_1, envStatus=1, projectId=null, project=[Project [projectName=Magic Matrix, projectDescription=Magic Matrix is a significant and robust tool of the Solitera Software Company., projectId=23]]], EnvironmentModel [environmentId=346, environmentName=MM_Env_2, environmentDesc=Environment_2, hostAddress=null, environmentURL=null, configFileName=soapAPI_request_GetLyrics.xml, configFilePath=/env_files/MM_Env_2, companyId=1, createdBy=2, createdAt=2019-12-18 16:43:10.59, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:43:58.522, elementFileName=Element.xml, elementFilePath=/env_files/MM_Env_2, envStatus=1, projectId=null, project=[Project [projectName=Magic Matrix, projectDescription=Magic Matrix is a significant and robust tool of the Solitera Software Company., projectId=23]]], EnvironmentModel [environmentId=347, environmentName=TM_Env_1, environmentDesc=TM_Env_1, hostAddress=null, environmentURL=null, configFileName=showcasetestData.xml, configFilePath=/env_files/TM_Env_1, companyId=1, createdBy=2, createdAt=2019-12-18 16:44:33.783, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:44:47.09, elementFileName=soapAPI_request_TempConvert.xml, elementFilePath=/env_files/TM_Env_1, envStatus=1, projectId=null, project=[Project ...
Length of environments : 2235
Environment Name : undefined
Use Jackson json parser in Java class and in javascript side use JSON.parse to change the string to JSON object.
On the javascript side you'll should understand that the variable environmentList is a string and the length of the string is 2235 chars. To access each property you'll have to parse the string and turn it into an object.
As you are not relying on JSON you need to use String.split or a regex matcher.
But the simpler solution would be to convert the Environment variables into a JSON format. As the type of the output from System.getenv() is a Map<string, string> you can try this solution to create a JSON structure: Converting Map<String,String> into json
After that you'll either write the json in a script tag in HTML so it get's parsed directly, or user JSON parse on the string of the above solution.

Concat QML Properties with Javascript

I'm setting properties on QML items with java script, where "gaugetext" is a property of my QML object.
function finishCreation(setWidth,setHeight,setX,setY,setMinValue,setMaxValue,setDecPlace,setUnit,setID,SetValueObject,SetValueProperty) {
...
"gaugetext": Qt.binding(function(){return SetValueObject[SetValueProperty] + " " + setUnit})
....
}
This works well, but I want to set numbers of decimals. So I tried this:
"gaugetext": Qt.binding(function(){return "(" + SetValueObject[SetValueProperty]+ ")" + ".toFixed(" + setDecPlace + ")" + " " + setUnit})
But this results that the correct Value is shown with ".toFixed(value)".
You return a String "(<someValue>).toFixed(<someOtherValue>) <someThirdValue>".
You probably want to omit some of your " since you probably want to have the pieces that you mark as string to be executed instead.
return SetValueObject[SetValueProperty].toFixed(setDecPlace) + " " + setUnit
~~~(untetested)
Most likely however using a "finalize creation" function is a bad approach.

error due to single quote in a parameter in url query string

I have a screen from which I am passing a parameter to other screeen. This parameter contains a single quote. (For eg. ABC's xyz). The other screen shows this paramter correctly on screen. However, I am not able to submit the request on load to servlet. I am not able to figure out why?
Tried using URL encoding, replacing ' with \' and replacing ' with %27. Didn't work for me. Plz help.
The code is as follows.
child page var scheme = document.forms[0].scheme.value;
window.location.href = redirectVal+".jsp?levelOfReport="
+ levelOfReport
+ "&circleCode="
+ circleCode
+ "&networkCode="
+ networkCode
+ "&moduleCode="
+ moduleCode
+ "&regionCode="
+ regionCode
+ "&branchCode="
+ branchCode
+ "&circleName="
+ circleName
+ "&moduleName="
+ moduleName
+ "&branchName="
+ branchName
+ "&Amount="
+ Amount
+ "&scheme="
+ scheme + "&fromDate=" + fromDate + "&toDate=" + toDate;
Child page
SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=<%=request.getParameter("scheme")%>
You're missing the end to your method call.
SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=<%=request.getParameter("scheme")%>
Map your quotes to eachother, you're telling it to do something like
SubmitButton('./SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=HTTP
You need to add an additional '); at the end of that line

Pass multiple parameters from ASP.NET to javascript function

Essentially what I'm trying to do is fairly simple....pass more than one parameter (4 total eventually) into a javascript function from my ASP.NET code behind.
What I've tried to do is this in the ASCX file...
function ToggleReportOptions(filenameText, buttonText) { /*stuff happens here*/ }
and this in the ASCX.cs file...
string testing123 = "testStringOne";
string testing124 = "testStringTwo";
optReportOptionsRunRealTime.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
but this doesn't seem to work as in my output the first variable contains "testStringOne, testStringTwo" and the 2nd variable is "undefined"
Any help on clearing up my probably stupid issue here would be great (i'm very inexperienced with javascript, more of a .NET developer)
You've missed out a few single-quotes, meaning that you're passing a single string containing a comma rather than two separate strings. That is, you're passing 'testStringOne, testStringTwo' rather than 'testStringOne' and 'testStringTwo'.
Try this instead:
optReportOptionsRunRealTime.Attributes["onClick"] =
"ToggleReportOptions('" + testing123 + "', '" + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] =
"ToggleReportOptions('" + testing123 + "', '" + testing124 + "')";

Categories

Resources