Skip to content Skip to sidebar Skip to footer

How To Create A Script With A Function Name Using ScriptElement.innerHTML = JSON.stringify?

I am modifying a couple of public widget api's that return some stock symbol data. I modified the call so I can populate the 'symbol' value when a user clicks on a link. The first

Solution 1:

You are going at it the wrong way. Since you are inside of JavaScript land you don't need to create a new script file to run something. Wait for the script to load and then execute your function.

I've taken the liberty to rewrite your script using jQuery, it does basically the same with the added layer of listening when the script loads. When the script is loaded the abc.widget is created with the symbol that is passed.

The function returns the script element in a jQuery object.

function createWidgetScriptElement(symbol) {
  var $scriptElement = $('<script>');
  $scriptElement.prop('async', true);
  $scriptElement.prop('src', 'https://abcstocks.com/external-embedding/embed-widget-technical-analysis.js');
  // Create `abc.widget` after the script is appended and loaded.
  $scriptElement.on('load', function() {
    new abc.widget({
      "width": 980,
      "height": 610,
      "symbol": symbol,
      "interval": "D",
      "timezone": "Etc/UTC",
      "theme": "light",
      "style": "1",
      "locale": "en",
      "toolbar_bg": "#f1f3f6",
      "enable_publishing": false,
      "allow_symbol_change": true
    }
  }
  return $scriptElement;
}
$('.mymenuitem').on('click', function(e) {
  var symbol = $(this).attr('data-href');
  var $widgetContainer = $('#widget_market-access_home');
  var $scriptElement = createWidgetScriptElement(symbol);
  $widgetContainer.empty();
  $widgetContainer.append($scriptElement);
  e.preventDefault();
})

Post a Comment for "How To Create A Script With A Function Name Using ScriptElement.innerHTML = JSON.stringify?"