The code works properly this way:
<script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-symbol-info.js" async>
{
"symbol": "BIST:ADEL",
"width": "100%",
"locale": "tr",
"colorTheme": "dark",
"isTransparent": false
}
</script>
But I have to use variables and it doesn’t work that way:
<script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-symbol-info.js" async>
{
"symbol": "BIST:" + localStorage.getItem('code'),
"width": "100%",
"locale": "tr",
"colorTheme": "dark",
"isTransparent": false
}
</script>
Error:
embed-widget-symbol-info.js:5 Widget settings parse error: SyntaxError: Expected ‘,’ or ‘}’ after property value in JSON at position 32 (line 2 column 31)
Invalid settings provided, fall back to defaults
What should i do?
I checked whether localStorage.getItem(‘code’) variable is defined. Absolutely defined. I need to properly print the name in the variable into JSON data.
2
Answers
It looks like you’re trying to concatenate a string with the result of localStorage.getItem(‘code’) within the JSON object, which is not allowed in this context.
Instead of doing concatenation within the JSON object, you should create the JSON object separately and then stringify it before passing it to the script.
The problem with this is – you are not in a JavaScript context there, with that last line.
The content between the
<script>...</script>
tags only gets interpreted as JavaScript, if the script element does not have asrc
set. As soon as it does, everything between the tags gets ignored.The script that was loaded via the
src
attribute can then go and find thescript
element in the DOM, and extract the content, as text. And what it then does with that text, is up to the script.The script here apparently expects something it can directly parse as JSON – but you now fed it something, that simply isn’t valid JSON.
I’m going to borrow from the other answer here, adding the necessary part: