Note that I ONLY get this error when onclick is called on an item that contains string with multiple lines, so I narrowed it down to this probably having something to do with new line characters. I am stuck as to how to fix this problem.
All information is loaded from my database. Here is an example of text that is loaded from database that is giving me this error because it has multiple lines (refer to longDescription field):
Here is how my source looks like near the error (note that the longDescription text starts right above line 264):
Here is the error message in console:
Here is the function:
Here is where it is being called:
Once again note that is works when there is no apparent new lines in the strings. Please help.
I used the following statement to properly deal with the new line characters. Don't be fooled by php's function nl2br() because it only inserts <br/> in front of new line characters, it does NOT replace new line characters.
$longDescription = preg_replace("/\r\n|\r|\n/",'<br/>',$longDescription);
Related
How to escape variables from DB like ${HOST}?
The situation happens when there is some code posted in tags <pre><code>.
If it remains as it is there's the error
Uncaught ReferenceError: HOST is not defined.
It's needed in a context where it's required to use the character ` for enclosure to prevent the error (it's a multiline content)
Uncaught SyntaxError: '' string literal contains an unescaped line break
The line generating error it's
e.setContent(escape(`<? echo $getpost["post"]; ?>`));
It gives the same error also by using
htmlspecialchars(addslashes())
This line:
e.setContent(escape(`<? echo $getpost["post"]; ?>`));
ends in your code as:
e.setContent(escape(`bla
bla2
bla3
`));
Because the $getpost["post"] contained newlines.
And your javascript isn't happy about the newlines.
Solution?
That depends on what you are trying to achieve, but you have to escape your newline somehow.
For example, replace it by \n, or NEWLINE and replace it back later.
Hard to say, but the reason for your error is the newline
I am Trying to retrieve data from json in my code. Json data doesn't contain any brackets. It is just any array separated by commas(,). My Problem is that when I get data using $scope, an extra sqaure bracket get added outside my output. Demo of my code is
controller.js
$http.get("serverUrl")
.success(function(data) {
$scope.sour = data;
})
.error(function(err) {
console.log("Error in source: "+JSON.stringify(err));
});
html
<div>
{{sour}}
</div
expected json
data
error
[data]
I have tried old stack solutions but none of them worked out for me.
Please share if anyone know why this error being produced, As I have used this method a hundred times before but never faced this problem.Regards.
After trying found out a solution.
.toString().replace();
solved out my problem.
As per how JSON.stringify should work as specified here, the result that you explained is as expected.
Let separator be the result of concatenating the comma character,
the line feed character, and indent.
Let properties be a String
formed by concatenating all the element Strings of partial with each
adjacent pair of Strings separated with separator. The separator
String is not inserted either before the first String or after the
last String.
Let final be the result of concatenating "[", the line
feed character, indent, properties, the line feed character,
stepback, and "]"
Which will result in a valid JSON String. The format you are requesting is confusing and is probably an invalid JSON. You can try checking the JSON using some JSON validator online. (Notice that [1,2,3,4] is valid where as 1,2,3,4 is invalid)
You are probably having a service that is expecting a malformed JSON, it will be better to fix that part instead of duck taping your JSON.
Meanwhile, the only thing you are explaining is the format you are getting and the format that is working. You haven't still specified where exactly the error happens? Is it error from HTTP request? Is it error thrown in Javascript? What is causing the error? What error messages are you getting?
I have a question:
I have a QR Code Scanner which gets a VCard. Im getting back the following String:
BEGIN:VCARD VERSION:2.1 FN:John Doe N:doe;john END:VCARD
The problem is the following: When putting this Information (via Javascript) in a textarea or just dump it to an alert winow, the information has breaks in between, looking like this:
BEGIN:VCARD
VERSION:2.1
FN:John Doe
.
.
.
But when putting this information into a paragraph (<p></p>), it has no breaks in it and it's just a plain string.
The question is now, can I put those breaks into the paragraph as well or can I insert another sign or anything else in between the attributes?
I thought about just splitting the string on blanks, but it's not working because e.g. FN itself contains blanks as wel...
Thanks for your help.
Edit:
Unfortunatelly, all 3 advices don't work..
What I have now is the following:
function writeqrcodecontent(){
var textfeld = document.getElementById("inhaltvonqrcode");
var wert = $.scriptcam.getBarCode();
//wert.split('\n').join('<br/>');
//wert.replace("\n", "<br>");
//wert.replace(/\n/g,'<br>');
textfeld.innerHTML = wert;
textfeld.style.display="block";
}
But as said, all three commented out lines do not work, everything is still displayed in one line.
The paragraph is defined with:
<p id="inhaltvonqrcode" style="display:none; clear:both;"></p
I understand your ideas but I don't understand why it's not working...
By the way, what I also tried is something like wert.split('\n').join('#'); to see, if it's really the break or it's just not working, but this doesn't work as well, so the text just seems to have no \n in it..
EDIT 2
It's working now, I neeeded to do both steps in one step, so not
var wert = $.scriptcam.getBarCode();
wert.split('\n').join('<br/>');
but
var wert = ($.scriptcam.getBarCode()).split('\n').join('<br/>');
Thanks for your help!
The string you get back likely contains newline characters \n. To get them to display with the newlines intact in HTML, you can either wrap everything returned with <pre></pre> in your HTML. Or split on newline \n and replace it with <br />.
text.split('\n').join('<br />')
text.replace(/\n/g,'<br>');
this line will replace all lines with <br />
Your barcode reader is reading the vCard with \n newline, however the pure html this newline is ignored.
in Javascript you can just use something like
someText.replace("\n", "<br>");
and it will do what you want. In php its the nl2br function
I'm writing a Photoshop Javascript script file. For all intents and purposes, this script when ran replicates a specific text layer several times. If the original text layer contains an apostrophe, the replicated instances replace the apostrophe with a square block. So "It's" becomes "It[]s" (obviously not brackets, but the square block.)
Here is the code:
titleLayer = al.textItem.contents;
newTitleLayer = titleLayer.replace("'", "\'");
alert(newTitleLayer); // At this point, this works: "It's"
persistentSetting.putData(0,newTitleLayer);
app.putCustomOptions("text_contents4",persistentSetting,true);
alert(persistentSetting.getData(0)); // At this point, it does not. It shows the square. "It[]s"
I know this has to be a simple issue, I've just never encountered this before.
Thanks.
I guess you want
newTitleLayer = titleLayer.replace(/'/g, "\\'");
// ^^^^ ^
// regex to match *all* apostrohpes escape the backslash
What ended up working for me is:
persistentSetting.getData(0).replace("EM", "'"); // It's not actually EM, but that's the little code that JS shows in my editor when I copied and pasted that special block [] character in.
Thanks for the help.
I'm prototyping some text processing to prep research data for coding, and I've got a javascript replace statement the bombs in jsFiddle and I cannot figure out why:
mE[1] = mE[1].replace(/<p.*>/ig, ''); // <<< this line
I'm trying to remove any opening paragraph tag.
If you look at http://jsfiddle.net/jotarkon/2e5gq/, uncomment that line and see that it the script fails.
-- click on the Heading to fire the funciton
This is driving me nuts. any ideas what's going wrong?
The problem appears to be an actual illegal character somewhere in that line, and I don't think it has anything to do with the regex. Try typing the whole line in from scratch and delete that one. When I do that, the fiddle works fine (well, it doesn't get that error at least).
edit — the illegal character is right after the semicolon on that line. Starting from the "//" on your "this line" comment, hit backspace a few times to erase the bogus character and the semicolon, then re-type the semicolon.
edit some more - The characters are the sequence C2 AD (hex).
First of all, don't use regexen for HTML. There are libraries available for that. You can't parse HTML with regexen. Second, you need to be more specific. Saying "a replace statement the bombs" tells us nothing about the nature of the error. Finally, in case you're curious, that regex is greedy, so it will replace everything from the first HTML tag that starts with the letter p until the very last > in your input indiscriminately. If you really want to use that, make it non-greedy and make sure it doesn't match other tags that start with the letter p. I'm not going to be specific because doing that is the Wrong Answer.