Passing script to innerHTML

Before you name it as a duplicate, I already looked here , here , but I can not find a working solution. I understand that I cannot pass the script to innerHTML due to the script tag. But is it possible to promote this particular API in a div without putting it on an HTML page. I have a button that accepts the input and runs the $ .get pair to get stock information, so I want to include this as part of the result, however innerHTML is not allowed, and I tried to execute many of these solutions in the links above, to no avail.
I can place the script below in the div on the HTML page, but I do not want to do this, plus I need to get the user input, so placing it inside my .js will be much easier to perform all functions together, I turned on the js widget.

<script type="text/javascript"> new TradingView.widget({ "width": 480, "height": 400, "symbol": "NASDAQ:AAPL", "interval": "D", "timezone": "Etc/UTC", "theme": "White", "style": "1", "locale": "en", "toolbar_bg": "#f1f3f6", "enable_publishing": false, "hide_top_toolbar": true, "save_image": false, "hideideas": true }); </script> 

I still have

 var script = document.createElement('script'); script[(script.innerText===undefined?"textContent":"innerText")] = "new TradingView.widget({ 'width': 580, 'height': 400, 'symbol': 'NASDAQ:"+ticker+ "','interval': 'D','timezone': 'Etc/UTC','theme': 'White','style': '1','locale': 'en','toolbar_bg': '#f1f3f6','enable_publishing': false,'hide_top_toolbar': true,'save_image': false,'hideideas': true});"; document.getElementById("stockChart").appendChild(script); 

And this work, but not the way I want, however, it is simple to execute that script, and this diagram is the only thing that appears on the page, is there a way I can make it be in a <div> ? With the above solution, the script is executed, but not placing the widget inside the div (it seems I could be wrong. I do not own this).

what it is doing In this image, when I run a script with a JS file with other operations, it just displays the graph on its own on the page.

when code is in html file This is when the code is inside the div and before running any JS functions, I think it is in the div, however I need to put the text entered above in the field so that all the information is displayed on one page, as shown below

where I want it to be I want the graph to be at the red dot, where <div> is

HTML

 <div id="content"> <img src="icon/logo.png"> <h1>Search Shares</h1> <div id="stockTickGet"> Ticker: <input type="text" id="tickBox" list="options"/> <button onclick="Ticker()" id="tickerSubmit" class="btn btn-primary">Submit</button> <datalist id="options"></datalist> </div> <div id='showStockTick' class='stockTicker'></div> <!-- <div id='stockChart'></div> --> <div id='stockChart'> </div> <div id='showStockSearch' class='stockTicker'></div> <div id='newsresult' class='stockTicker'></div> <button class="btn btn-primary" data-toggle="modal" data-target="#modal1">Buy Stocks</button> </div> 

In a way, this is what I want to do, but not allowed / works

 document.getElementById("stockChart").innerHTML = "<script> new TradingView.widget({ 'width': 580, 'height': 400, 'symbol': 'NASDAQ:GOOG','interval': 'D','timezone': 'Etc/UTC','theme': 'White','style': '1','locale': 'en','toolbar_bg': '#f1f3f6','enable_publishing': false,'hide_top_toolbar': true,'save_image': false,'hideideas': true});<"+"/script>"; 
+7
javascript jquery html
source share
1 answer

While a script can be placed in a div, the misunderstanding here seems to be that you can select the target div for any html created by the script.

Without looking at the script code, or at least knowing that the script does not allow you to tell whether the script creates any html elements or even if it is possible, you cannot know how it enters those that are on the current page.

new TradingView.widget(..) . Even if you place this as a script element on the html element on your page, it can dance jig and cook the stew in everything we know.

As for the solution to your problem:

I did a search for Tradeview.widget and found this page: www.tradingview.com. This seems to be what you are talking about. If you look at the source of the page, perhaps you can add container_id to the widget's parameters:

 new TradingView.widget({ ... container_id : "widget-frame-container"; }); 

This means that if you have a div with the given id:

 <div id="widget-frame-container"></div> 

Then I would suggest that whatever the script produces will be entered there.


Sorry for not testing and giving a semi-similar answer!

+2
source share

All Articles