How do I create line breaks in JavaScript? - javascript

I am using some code I found on this site and need to know how to insert a line break instead of a comma.
I have tried using \n as follows:
if(flg>0){
app.alert('There are '+flg+' fields that require a value\n\n'**+ fArr,3**)
}
But keep getting a syntax error. What am I doing wrong?

Adding the following text:
"\n"
on where you want the line break, definitely causes a line break. E.g. onclick="alert('Hi \n How are you?')"
You may be getting a syntax error because how you've entered asterisks. Try wrapping them with quotation marks too?

\n is what you require to get a line break and is correct. Your syntax error occurs because alert('There are '+flg+' fields that require a value\n\n'**+ fArr,3**) is not a valid method - unless you have overwritten it, alert() is a method that only takes one argument - a string.
'There are '+flg+' fields that require a value\n\n'**+ fArr,3** is not a valid string. Try instead 'There are '+flg+' fields that require a value\n\n**'+ fArr+',3**'.

Related

CL-WHO is always starting with a single quote

My issue is that CL-WHO begins each expression with a single quotation market when it turns the Lisp S-expressions into html output. This is okay most of the time, but it is an issue since I am linking my file to an external javascript file. I am trying to make this project simple, and since none of the javascript developers on my team know Common Lisp, using parenscript is likely out of the equation. Here is an example of my issue and one of the errors in my program:
:onclick "alertUser('id')"
When a particular element is pressed within the html document, this should trigger a JavaScript function called alertUser, and the id of the tag should be passed to the JavaScript function as an argument. But no matter what I do, CL-WHO will convert that string into single quotation marks, so I end up with an invalid expression. Here is what that code converts to:
onclick='alertUser('id')'>
Everything is a single quotation so 'alertUser(' is passed as the first string which is obviously invalid and I receive a syntax area in my developer tools. I thought that I could solve this problem by using the format function with escape characters. This would equate to:
CL-USER> (format t "\"alertUser('id')\"")
"alertUser('id')"
NIL
CL-USER>
But when I try that with CL-WHO:
:onclick (format nil "\"alertUser('id')\"")
That translates to:
onclick='"alertUser('locos-tacos-order')"'>
Which is also invalid html. As you can see, CL-WHO will start with a single quote no matter what. Next I tried the CL-WHO fmt function:
:onclick (fmt "\"alertUser('locos-tacos-order')\"")
When I use the fmt function it gets rid of my :onclick expression entirely when it is converted to html!:
id='id'"alertUser('id')">
Lastly I tried the str function, and got similarly invalid output to my original attempt:
onclick='"alertUser('id')"'
Obviously if I code this in pure html it will look like:
onclick="alertUser('id')">
Which is valid.
My question is simply how do I enable CL-WHO to use double quotation marks in these situations instead of single quotation marks?
#jkiiski was has the correct answer in the comments underneath my question, but I wanted to post the answer so that anyone with a similar issue in the future can resolve the problem. As #jkiiski said, there is a variable called ATTRIBUTE-QUOTE-CHAR in the cl-who package that defaults to #\'. You can simply set that variable to #\" instead in order for the default quotations used to be double quotation marks:
(setf *attribute-quote-char* #\")
After adding that line of code near the top of the file my html defaults to:
onclick="alertUser('id')"
and now the javascript can execute properly. Credit to #jkiiski for a correct answer.

How can I use a Mirth-Javascript to remove line breaks in HL7 messages?

An HL7 message comes into Mirth and throws a "processing" error. At the very bottom of the message in Raw format is a partial line that has been separated from the line above it. I have to manually correct this every time. I am hoping to use a Mirth-Javascript as a message filter that can fix this so that everything flows without human intervention.
Below message snippet triggers the error. In this example it is the very last line of the HL7 message.
OBX|68|FT|PT6663&IMP^PET/CT Imaging Whole Body||
||||||F|||202254836969552|||
Currently my only fix is to open the HL7 message and manually go to the line break and bring it up to the line above it that is part of the segment.
The HL7 message should look like this:
OBX|68|FT|PT1103&IMP^PET/CT Imaging Whole Body||||||||F|||20190327101958|||
This worked. Put the following into the preprocessor.
message = message.replace(/[\r\n]+(?![A-Z][A-Z][A-Z0-9]\|)/g, "");
return message;
Remove all line brakes in the channel's pre-processor or attachment script, and then insert them back based on the segment names.
The best way would be to stop the message generating system insert line brakes into OBX.5 field.
Removing all line breaks would be an approach, but it could be a problem later on, you could set up a replace script, that instead of '/n', searches for '|/n|' or a similar string, that way, it would fix that particular problem as well as any other undesired line breaks in between vertical separators, tho it wouldnt help if it broke anywhere else, so keep that in mind.
Put this code snippet in your preprocessor script. It worked for me
var newmessage = message.replace(/[\n\r]$/,"");
while (newmessage.match(/(\r\n|\r|\n)([^A-Z]|[A-Z][^A-Z]|[A-Z]{2}[^A-Z\d]|[A-Z]{2}[\d][^|]|[A-Z]{3}[^|])/i)) {
var extrabit = newmessage.match(/(\r\n|\r|\n)([^A-Z]|[A-Z][^A-Z]|[A-Z]{2}[^A-Z\d]|[A-Z]{2}[\d][^|]|[A-Z]{3}[^|])/i)[0].substring(1);
var newmessage = newmessage.replace(/(\r\n|\r|\n)([^A-Z]|[A-Z][^A-Z]|[A-Z]{2}[^A-Z\d]|[A-Z]{2}[\d][^|]|[A-Z]{3}[^|])/i,'\\.br\\' + extrabit);
}
return newmessage;
Mirth processor expects every line the first 3 characters should contain valid HL7 segments otherwise the mirth throws an error.
To remove the invalid line breaks in the HL7 message you should follow the below steps.
1.Channel -->Scripts -->Preprocessor.
Paste the bellow code top of the "return message;" statement
message = message.replace(/[\r\n]+(?![A-Z][A-Z][A-Z0-9]\|)/g, ""); //This Line is for invalid line breaks in incoming message APPEND to PREVIOUS SEGMENT .
Save the changes and deploy the channel for new changes affected.
From your question, the HL7 field that contains line breaks is OBX(5,1) which should hold Observation Value.
Observation value may contain line breaks as a part of data. Line break (<CR> or ASCII 13) is segment separator by default. If this is received as a part of data, there will be issues while parsing message. This is the root cause of the problem you mentioned in the question.
The segment separator is not negotiable. It is always a carriage return. I have explained this in more details in this answer.
Ideally, those line breaks should be replaced with its escape sequence while building HL7 message. More details about it are already given in one of my earlier answers here.
So, your inbound message
OBX|68|FT|PT6663&IMP^PET/CT Imaging Whole Body||
||||||F|||202254836969552|||
should be actually
OBX|68|FT|PT6663&IMP^PET/CT Imaging Whole Body||\X0D\\X0D\||||||F|||202254836969552|||
About your actual question that how to do this with Mirth/Javascript, there should not be need in your particular use case. This conversion should be done before sending message to Mirth. So, the one who is sending this message to you should build it like this.
While actually displaying observation value on UI, you again need to do the reverse process.
Edit:
If line break is different than <CR> (ASCII 13), then respective HEX should be replaced in \X0D\. Details are mentioned in my linked answer; I am not repeating those here.
I had the similar issue of having blank lines between the segments and i solved it liked this :
content = content.replace(/^\s*\n/gm, '');
Note: This will just remove blank lines. You need to still figure out how to get the next line on current line
You can try regex to eliminate all '\n' not followed by any segment.

Illegal character in javascript

I've been debugging this for hours already but really can't find the culprit of this illegal character. My javascript looks fine. This is my code.
this.PrintApplication = function Test$PrintApplication(ApplicationID, callback) {
$.post("/Application/PrintApplication/" + ApplicationID,
function (data) {
var result = eval(data);
if (result.error) {
DisplayPrompt("Error", result.message);
return;
}
else {
callback(result.data);
}
});
};
In firebug it shows.
In inspect in chrome and in console it redirects me in this line.
Any idea where is that illegal character is in my function?
It looks like you've got some unprintable characters in your source. Do you have a way of displaying those in your editor and deleting them? Deleting and retyping the line might fix it as well.
If that's not the case, maybe what you're trying to evaluate isn't JavaScript at all. You could be running that on an image or some kind of binary data.
Remember to be extra super careful when using eval on data that comes from an external source. If you can avoid doing it, avoid it.
This might be due to the reason that you have copied the code from web and simply pasted in your file. Try typing the same code to the file.
This error occurs due to UTF-8 characters.
This could happen if you normally type with different alphabets. For example the Γreek question mark ; is a different ASCII character from the English semi colon ;. If you use the first, you'll get exactly this error.
One solution is to copy paste your method to notepad and then back to your IDE.
This will often normalise and eliminate weird characters that might be hidden or undecipherable.

Javascript Newline Character Throwing Error

I am having trouble getting some data from a textbox using javascript. What I am trying to do is take the value from a textbox and put it in an alert and copy it. Here is the code that I currently have:
var copyString = "Date: <%= TXT_Details_DateReq.Text %>;
window.prompt('Press CTRL+C, then ENTER\n\nNOTE: SAVE ANY CHANGES BEFORE COPYING TEXT!', copyString); return false;
So this code works perfectly fine if the text in the textbox is just one line. But if the text in the textbox has multiple lines such as:
"This is one line
here is a second line"
The code will throw the error Uncaught Syntax Error: Unexpected Token ILLEGAL.
From what I have researched this throws when there is an illegal character, so I believe it is the CRLF character from the textbox.
Any idea how to fix this?
Thanks.
New line characters might be preceded by return characters ('\r').
Swap '\n' with '\r\n' and you should be good to go.
Or even better: just handle all cases of the new line character instead of checking which case then applying it. This replace the newline:
htmlstring = stringContainingNewLines.replace(/(\r\n|\n|\r)/gm, "<br>");
Resource
hope it helps.

add class to parent on click with jQuery

I currently have this, it was working in jsFiddle, though its giving me errors, and not working when I use it outside of fiddle.
$('.down-photo').click(function() {
$(this).parent('.img-mask').toggleClass('hide');
});​
Firebug says:
Uncaught SyntaxError: Unexpected Token ILLEGAL
I'm newer to javascript and jQuery so I'm not sure what's wrong or what that error means.
Thanks!
In the code in your question there is a non-printing character after the last semicolon. It seems to be character 8203, a Unicode zero-width space. That's the illegal token that Firebug is telling you about.
You'll notice, if you edit the text in your question, that if you put the cursor to the left of that last semicolon and then press the right arrow a few times it takes one more keypress than you'd expect to get to the next line - that's the character I mean.
Delete that character, or manually retype the line (rather than copy/pasting), and it should be fine.
I fixed your error. $.appendTo() takes a selector, not a jquery object.
Try this:
http://jsfiddle.net/dvCmR/133/ (line 4)

Categories

Resources