Convert unicode from Json in HTML with Javascript - javascript

I am getting issues to convert unicode and render in a nice HTML code.
Here is the information i have as an input in my Json file
`"CLIENT:\r\n-Client1: Project1\u00c2\u00a0\r\n- Client2: etc..."`
I would like this to render as below in
CLIENT:
- client1: Project 1
- Client2: etc...
It currently renders like this :
`CLIENT: - Client1: Project1Â - Client2: etc...`
I looked everywhere but could not find a function that could handle all unicodes to decode in nice html code.
Thanks in advance!

Maybe you can take a look at this:
How do I replace all line breaks in a string with tags?
You do this:
str = str.replace(/(?:\r\n|\r|\n)/g, '<br />');
before insert into the html.

If you are using javascript, you can use this fragment
<script>
var c = decodeURIComponent(escape(`"CLIENT:\r\n-Client1: Project1\u00c2\u00a0\r\n- Client2: etc..."`));
c = c.replace(/(?:\r\n|\r|\n)/g, '');
c = c.replace(':-', ': - ');
document.write(c);
</script>

Ok I managed to make this work with the below function:
function strFormat(str) {
var c= decodeURIComponent(escape(str));
c= c.replace(/(?:\r\n|\r|\n)/g, '<br />');
c= c.replace(':-', ': - ');
return c;
}
Let me know if there are any cleaner way to do this?

Related

Server side fix for widows in text string

I'm having an issue with server side widows fix.
I'm able to fix widows/orphans with client side javascript.
However I would prefer to do it server side before the page renders. Unfortunately my C# is limited. How would I be able to accomplish this? Or at least get an idea of how to accomplish this.
Here's the javascript that I've used.
var wordArray = $('element').text().split(' ');
if (wordArray.length > 1) {
wordArray[wordArray.length - 2] += ' ' + wordArray[wordArray.length
- 1];
wordArray.pop();
$(''element'').html(wordArray.join(' '));
}
Thank you in advance.
Please read my comments inside the code
public string Model_fix(string ElementText)
{
var result = string.Empty;
// the compiler will set wordArray as List of string and not an array
// list in c# is more similar to an array in javascript
var wordArray = ElementText.ToString().Split(' ').ToList();
if (wordArray.Count > 1)
{
wordArray[wordArray.Count - 2] += "' '" + wordArray[wordArray.Count - 1];
wordArray.RemoveAt(wordArray.Count - 1); // equivilent to Array.pop in javascript
result = string.Join(" ", wordArray); // equivilent to Array.join() in javascript
}
return result;
}

Manipulate text from form - JavaScript/Jquery

Hello I am working on a project and last night I had a thought that would make a lot of what I am wanting to do a heck of a lot easier, the only problem is I am not sure on the best way to tackle it. Let me explain....
I have a form on a website where a user enters a VIP ID that is in a pre-determined format and follows a logical naming convention.
Example: app.prod.platform.org.dc1.prod.site.com-HTTP_80tcp-00000000-lb
I want to pull out the following information from the entered text.
prod.platform.org.
Then I want to reverse it logically
.org.platform.prod
And then I want to replace the “.” For “/”
/org/platform/prod
And finally I want to add a postfix of “/open*”
/org/platform/prod/open*
So in short,
INPUT = app.prod.platform.org.dc1.prod.site.com-HTTP_80tcp-00000000-lb
OUTPUT = /org/platform/prod/open*
I am using javascript/jquery for everything else but I am pretty new to all of this so I tend not to know the best route to tackle a problem. If I need to provide some more detail I can do. Any help is much appreciated.
Or simple like this
var input = "app.prod.platform.org.dc1.prod.site.com-HTTP_80tcp-00000000-lb" ;
var output =
"/" +
input
.split(".")
.slice(1, 4)
.reverse()
.join("/") +
"/open";
var output =
"/" +
"app.prod.platform.org.dc1.prod.site.com-HTTP_80tcp-00000000-lb"
.split(".")
.slice(1, 4)
.reverse()
.join("/") +
"/open";
You can try below code :
var input = "app.prod.platform.org.dc1.prod.site.com-HTTP_80tcp-00000000-lb";
var tempArr = input.split(".");
var newArr = new Array();
for(var i=1;i<tempArr.length;i++){
if(tempArr[i]=="org" || tempArr[i]=="net"){
newArr.push(tempArr[i]);
break;
}
newArr.push(tempArr[i]);
}
newArr.reverse();
var output="/"+newArr.join("/")+"/open*";

unexpected string in append() jquery

I'm trying to append a some HTML to a code in jquery, but chrome keeps throwing "unexpected string" at the append function, here's the code :
for (var i =0 ;i<resultArray.length;i++){
$.ajax({url:"getExpenseInfo.php",type:"POST",data : {
'expenseId' : resultArray[i]["expenseId"]
},success:function(expense){
expense = $.parseJSON(expense);
$("#mainDiv").append("<div class=\"row\">".expense["expenseName"]."</div>");
}});
}
I guess you're mixing up php and js syntax, string concatenation works with + in js and not ..
$("#mainDiv").append("<div class=\"row\">" + expense["expenseName"] + "</div>");
Your string concatenation in your append function is a little off. Try something like this:
for (var i =0 ;i<resultArray.length;i++){
$.ajax({url:"getExpenseInfo.php",type:"POST",data : {
'expenseId' : resultArray[i]["expenseId"]
},success:function(expense){
expense = $.parseJSON(expense);
$("#mainDiv").append("<div class=\"row\">"+expense[expenseName]+"</div>");
}});
}

JQuery / Javascript & Split within split?

A Caveat: As a dilettante I lack the vocabulary to precisely describe my problem. Bear with me.
Let's assume I have a string like the following one:
A1%%A2%%A3%%A4||B1%%B2%%B3%%B4||C1%%C2%%C3%%C4||D1%%D2%%D3%%D4
Using JavaScript / JQuery I would like to split it twice (using '||' and '%%' as delimiters) and have the following HTML as output:
<p><input value="A1"></input><input value="A2"></input><input value="A3"></input><input value="A4"></input></p>
<p><input value="B1"></input><input value="B2"></input><input value="B3"></input><input value="B4"></input></p>
<p><input value="C1"></input><input value="C2"></input><input value="C3"></input><input value="C4"></input></p>
<p><input value="D1"></input><input value="D2"></input><input value="D3"></input><input value="D4"></input></p>
I know how to do basic splitting and joining, but this goes beyond my comprehension.
You can use something like this.
s = "A1%%A2%%A3%%A4||B1%%B2%%B3%%B4||C1%%C2%%C3%%C4||D1%%D2%%D3%%D4";
s2 = s.split("||");
s2.forEach(function(item){
s3 = item.split("%%");
s3.forEach(function(item2){
console.log(item2);
});
});
You can use replace
function splitString(match, part) {
switch (part) {
case '%%':
return '"></input><input value="';
case '||':
return '"></input></p>\n<p><input value="';
}
}
var formattedValue = 'A1%%A2%%A3%%A4||B1%%B2%%B3%%B4||C1%%C2%%C3%%C4||D1%%D2%%D3%%D4'.replace(/(%%|\|\|)/g, splitString);
formattedValue = '<p><input value="' + formattedValue + '"></input></p>';
console.log(formattedValue);
All split joins. It will be fast.
var str = 'A1%%A2%%A3%%A4||B1%%B2%%B3%%B4||C1%%C2%%C3%%C4||D1%%D2%%D3%%D4',
result = str.split('||').join('"></input></p>\n<p><input value="');
result = '<p><input value="'+
result.split('%%').join('"></input><input value="')+
'"></input></p>';
console.log(result);
//or
//alert(result);
//
With problems like this don't think so much about what format you want.
Start with something simple first like:
var result = str.split('||').join('><');
result = str.split('%%').join('><');
Then you might see how to use the longer string to get what you actually want.

How can I write data in a single line on text file using javascript

I am trying to write data in a .txt file using JavaScript function.
My function :-
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("test.txt", true);
for(var j=1;j<9;j++)
{
if(document.getElementById('chk'+j).checked)
{
s.WriteLine(' ');
s.WriteLine(document.getElementById('item'+j).innerText);
s.WriteLine(',');
s.WriteLine(document.getElementById('txtBx'+j).value);
s.WriteLine(',');
s.WriteLine(document.getElementById('txtBxx'+j).value);
s.WriteLine(';');
}
}
alert("written");
s.Close();
}
Now the problem is that data being written on a new line.
(maybe because i am using s.WriteLine ?)
I want all the data to be written in a single line. how can i achieve this?
to elaborate more, my current output looks like this
abc
,
1
,
cdf
;
def
,
3
,
ehi
;
I want it like this-
abc,1,cdf;def,3,ehi;
Use s.write() instead of s.writeLine(). As the name implies, the second function is for writing whole lines, and it adds a newline after it.
Just add everything to a variable then write the line.
var st = document.getElementById('item'+j).innerText;
st += ','
st += document.getElementById('txtBx'+j).value;
s.WriteLine(st);

Categories