Reputation: 21212
Seeking some cheeky on the fly help here. Been staring at this for along time and cannot see why the whole thing is not maintaining being a string:
var str =
'<script type="cats/conversion">
{"type":"REGISTER",
"params":{"partner_conversion_id":"' + {{sku}} + '",
"f":"' + {{accountHolderName}} + '",
"e":"' + {{dl userid}} + '"}
}
<\/script>';
I had expected that all of str is a string but my text editor is telling me that it's not. Where have I typeod a ' or "?
Help!!
Upvotes: 0
Views: 91
Reputation: 69
You either do it in one line
var str ='<script type="cats/conversion"> {"type":"REGISTER", "params":{"partner_conversion_id":"' + {{sku}} + '", "f":"' + {{accountHolderName}} + '", "e":"' + {{dl userid}} + '"}} <\/script>';
For readability if you want it to be multi lined you have to concatenate the string.
var str = '<script type="cats/conversion">' +
'{"type":"REGISTER",' +
'"params":{"partner_conversion_id":"' + {{sku}} + '",' +
'"f":"' + {{accountHolderName}} + '",' +
'"e":"' + {{dl userid}} + '"}' +
'}' +
'<\/script>';
Upvotes: 0
Reputation: 24
I think you mean to do this:
var str =
'<script type="cats/conversion">
{"type":"REGISTER",
"params":{"partner_conversion_id":"' + '{{sku}}' + '",
"f":"' + '{{accountHolderName}}' + '",
"e":"' + '{{dl userid}}' + '"}
}
<\/script>';
The {{}}'s need to be wrapped in quotes.
Upvotes: -1
Reputation: 44833
You can't (safely and reasonably) do multi-line string literals in JavaScript. You have to build them up using concatenation (the +
operator). You can do it like this:
var str = '<script type="cats/conversion">' +
'{"type":"REGISTER",' +
'"params":{"partner_conversion_id":"' + {{sku}} + '",' +
'"f":"' + {{accountHolderName}} + '",' +
'"e":"' + {{dl userid}} + '"}' +
'}' +
'<\/script>';
Note: I have no idea what the {{sku}}
and similar placeholders in your script are. I assume those are for some templating system. They are not, as written, valid syntax.
Edit: Technically, as Alex pointed out in his answer, you can do multi-line literals by escaping the newline with \
. As Alex says, however, this is "considered horrible practice" and prone to breaking for all kinds of reasons. Don't do it!
Upvotes: 5
Reputation: 17952
In Javascript you have to add a \
to the end of a line to indicate a multi-line string. This is considered HORRIBLE practice, because if the \
is followed by any whitespace, it will throw a syntax error.
var ok = "this is \
my multiline string!";
var notOk = "this is \
my multiline string!";
The 2nd is an error because there is a space after the \
at the end of the line.
If you are using ES6, you can use template strings with backticks instead:
var x = `This is my
multiline string!`;
If ES6 is not an option and you don't want to follow bad practices by using \
, concatenate multiple strings together or insert newlines in your string like so:
var myString = "this is line 1" +
" this is line 2" +
" this is line 3";
or
var myString = "this is line 1\n this is line 2\n this is line 3";
Upvotes: 0
Reputation: 750
Try this
var str = '<script type="cats/conversion">'+
'{"type":"REGISTER",'+
'"params":{"partner_conversion_id":"' + sku + '",'+
'"f":"' + accountHolderName + '",'+
'"e":"' + dl_userid + '"}'+
'} '+
'<\/script>';
Upvotes: 0