jade syntax error how to send json to js ? - javascript

a(href="#" id="pickndroppoints" data-pickuppoints="[{"Id":"HGHD","Time":"2014-01-28 21:00:00.0","Name":"Beach Road"}]")
jade compile give systax error Unexpected identifier
Is there a way to do this ? I need to send json and based on it certain event is triggered using js

your attribute value for data-pickuppoints is breaking at.
data-pickuppoints="[{"Id":"HGHD","Time":"2014-01-28 21:00:00.0","Name":"Beach Road"}]"
as you have double quotes inside the json data. make it like
data-pickuppoints='[{"Id":"HGHD","Time":"2014-01-28 21:00:00.0","Name":"Beach Road"}]'
or escape the double quotes

I would say, as jade parameter are regular javascript, that you only have to use single quotes so the value wont break :
a(href="#" id="pickndroppoints" data-pickuppoints='[{"Id":"HGHD","Time":"2014-01-28 21:00:00.0","Name":"Beach Road"}]')
But I am not on my computer so I cant test.

Related

How can we escape all double quotes in prestashop?

I'm using prestashop as a cms and i want to display specific products on a page.
I found a module to do so by writing for example this line of code : [ph-product-cms id="1"] which will be converted at the execution into many lines of code with multiple quotes to display the products
The thing is when i try to use innerhtml like this : document.getElementsByClassName('test0 prod')[0].innerHTML = "[ph-product-cms id="1"]"; it shows Uncaught SyntaxError : Unexpected identifier
I tried single quotes but it doesn't work either.
Wherever you find double-quotes put a \ before them to escape them. For example,
\[ph-product-cms id=\"1\"]

Json response with a variable value

Hi, I am trying to get a message to say the value of the data's user id as in line 95. However, the json message isnt recognizing it as a variable and is just printing that line how it is. I am unfamiliar with the syntax here, could somebody help me out? Thanks.
If you want to include variables in text strings using JavaScript, you will have to delimit the string with backticks. This character: `
`user ${data.user.uid} signed up successfully`
The single quote / apostrophe you're using now does not allow for inclusion of variables.

AngularJS format string inside double braces using replace() with regex

I am trying to format a MAC address in a table that is populated using AngularJS:
<tr ng-repeat="dev in devices">
<td>{{dev.mac.replace(/(.{2})/g,"$1:")}}</td>
</tr>
{{dev.mac}} works just fine (aside from being unformatted), but when I add the .replace() function it breaks. I tried escaping the forward slash based on the error I received, which didn't help. Is .replace() not available inside the browser or is there a different syntax for regex inside the double braces or what am I doing wrong?
The goal is to convert AABBCCDDEEFF into AA:BB:CC:DD:EE:FF as easily as possible within the double braces. As a bonus question, how do I prevent the trailing ':' in the regex (it currently prints AA:BB:CC:DD:EE:FF:)?
Edit: Adding the error message
Error: $parse:syntax
Syntax Error
Syntax Error: Token '/' not a primary expression at column 20 of the
expression [dev.mac.replace(/(.{2})/g,"$&:")] starting at
[/(.{2})/g,"$&:"].
That seems to indicate the forward slash is causing the problem, but like I said, escaping it doesn't help.
Rather than running your replace inline like that, it'd be better to abstract that out into a function, that should fix any issues you're having with it not getting interpreted correctly. This article shows the right syntax for declaring a function on the scope to call here: function call inside Angular double curly braces, it should be something like this
$scope.fixMacAddress = function(addr)
{
return addr.replace(/(.{2})/g,"$1:")
}
and
{{ fixMacAddress(dev.mac) }}

Unterminated string constant-mshta:javascript

Recently I was trying to get a quick alert box from javascript using mshta but I noticed something strange and I have no ideea what the problem is. This is,in a way,what I was trying to achieve:
mshta javascript:alert("The file was stored here:\"C:\\folder_with_space_ _.txt");
The error it gives is the one in the title of this post(char 57).I tried a combination of things and:
//code that works:
mshta javascript:alert("The file was stored here:\"sdadasd");
mshta javascript:alert("The file was stored here:\"\" sdadasd");
//error-notice the space;error on char 35
mshta javascript:alert("The file was stored here:\" sdasds");
It looks like it's giving error when the number of double-quotes is odd,but:
//error
mshta javascript:alert("The file was stored here:\" \"sdadasd");
I tried to do the same in a browser console and it worked. I believe is some kind of parser-error.How can I fix it?(I am thinking of using fromCharCode to directly insert the double quote).
Note: the commands were run from cmd.
I'll start off with the version of the command that I got to work, and I'll explain why it works:
mshta "javascript:alert('The file was stored here:\x22C:\\folder_with_space_ _.txt');"
The first and perhaps most important point is that we are passing a single argument to mshta.exe (the JavaScript command to execute), so we should surround that entire argument in double quotes. This prevents the space from being treated as an argument delimiter.
The second point is that there doesn't seem to be a way to have double quotes inside the actual JavaScript commands. According to the question Escaping Double Quotes in Batch Script, there is no standard for escaping double quotes inside double quotes for cmd. Apparently, mshta.exe doesn't honor "" or \" (or at least, I couldn't get them to work). I suggest following Teemu's suggestion in the comments and use single quotes only for string delimiter in the JavaScript code. If, inside a string, you want to include a double quote character, use the hex literal \x22.

JavaScript string declarations from dynamic data with unescaped quotation marks

I am writing JavaScript templates for a content management system where users fill out text input fields that are passed to my templates.
My problem is the quotation marks in the input fields are not escaped before they are passed to my template, so I have no way of knowing if they will contain single or double quotes, or even both. Whichever way I try to handle the data my code ends up breaking because the quotes terminate the string declaration. I want to run a function on the data to escape quotes but I can't find a way to get the data into a valid variable first.
Is there any way to safely handle the data in JavaScript without it breaking a string variable declaration?
Edit: I'm posting code example;
CMS Text Input Field value is: Who'll win our "Big Contest"?
Text Input Field placeholder macro is [%TextInput%]
I'm building an HTML template for this input, using just JS/HTML/CSS
<script>
(function(){
var textInputStr = "[%TextInput%]";
})();
</script>
This will break the string declaration if the value of TextInput contains a single quote, and vice versa.
This is an awesome question, and one that deserves an answer. Strings in JS don't have a custom delimiter, like in most other modern languages, so you can get really stuck. But one solution is to build a script tag with the placeholder inside it, then find the innerHTML of that tag and grab the string back into a variable. eg
<script id="parseMe" type="text/template">
[%TextInput%]
</script>
then use
var yourString = document.getElementById("parseMe").innerHTML
Now you can manipulate the string as you please.
HTH!
I want to run a function on the data to escape quotes but I can't find a way to get the data into a valid variable first.
Well, you will have to make it a valid string literal before you could run JavaScript functions on it. There's no other way (unless you count an ajax request to the template script to get a string representation of it).
The input fields are not escaped before they are passed to my template
Then fix that. There's nothing you can do about it in JavaScript.

Categories