skip to Main Content

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


  1. 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.

    
    <script type="text/javascript">
        var widgetSettings = {
            "symbol": "BIST:" + localStorage.getItem('code'),
            "width": "100%",
            "locale": "tr",
            "colorTheme": "dark",
            "isTransparent": false
        };
    
    
    
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.async = true;
        script.src = 'https://s3.tradingview.com/external-embedding/embed-widget-symbol-info.js';
        script.onload = function() {
            new TradingView.widget(widgetSettings);
        };
    
    
    
        document.getElementsByTagName('head')[0].appendChild(script);
    </script>
    
    
    
    Login or Signup to reply.
  2. <script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-symbol-info.js" async>
              {
                "symbol": "BIST:" + localStorage.getItem('code'),
    

    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 a src 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 the script 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:

    <script type="text/javascript">
        var widgetSettings = {
            "symbol": "BIST:" + localStorage.getItem('code'),
            "width": "100%",
            "locale": "tr",
            "colorTheme": "dark",
            "isTransparent": false
        };
        
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.async = true;
        // encode the widgetSettings as JSON, and set as text content of the script element:
        script.text = JSON.stringify(widgetSettings); 
        script.src = 'https://s3.tradingview.com/external-embedding/embed-widget-symbol-info.js';
    
        document.getElementsByTagName('head')[0].appendChild(script);
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search