Dynamically changing the symbol
Once you’ve laid down the groundwork and embedded TradingView’s widgets into your webpage, you may want to dive deeper and gain more control over your page’s functionality. In this section, we’ll cover how we can dynamically change the stock symbol of interest and how to cleanly manage embed scripts with template elements.
So far, we have created a page for a single stock symbol (AAPL). However, we don’t want to restrict the fun to just one symbol. Let’s make it work for a wide range of symbols.
On the server-side, we can send different content depending on the page’s route (URL path) and modify the symbol names specified in the widget options.
For instance, if we want to display the details for the NVDA symbol instead of AAPL, we simply need to update the value of "symbol"
in the options.
Symbol names are defined using the syntax {EXCHANGE}:{NAME}
.
In this example, we will retrieve the desired symbol name from the query portion of the URL (the part that comes after the ?
). For instance, in the URL https://example.com/?symbol=NASDAQ:AAPL
, the query string contains the property “symbol” with a value of “NASDAQ:AAPL ”. Using JavaScript, we can extract the symbol name from the URL and dynamically set its value in the embed code before inserting it into the page.
We will use the default query parameter name of tvwidgetsymbol
to define the symbol name. The Advanced Chart widget will automatically detect this value and display the correct symbol on the page. 🎉
To handle the remaining widgets, we’ll use JavaScript to dynamically modify the symbol option. This is necessary because these widgets may be used multiple times on a single page, each with a different symbol. Relying solely on the page’s URL isn’t always suitable in such cases. Therefore, we need to include additional code on the website to handle this situation.
To retrieve the symbol value from the query string, you can utilize the following code:
Changing symbol within the Widget options
So far, we have integrated the widgets by directly placing the embed code into the page. As the page loads, the widgets automatically create themselves and replace the contents of the container div element within the embed code. As a result, the widget will begin loading with the default or predefined symbol name before we have an opportunity to read and set the new symbol name from the query string.
One way to address this is by utilizing template
elements to store the embed code. This code can be inserted into the document using JavaScript. By converting the embed codes into template elements, we can enhance the cleanliness and maintainability of the page code. These template elements can be cloned dynamically into the document. The content within a template
element is not executed or treated as live page content, but it can be used to dynamically insert the content into the page where it will be displayed.
So the changes we are going to make to the HTML source code are as follows. We are going to move the embed code into template elements, and leave the widget container elements as empty placeholders.
We will use the following naming convention: the placeholder container for the widget will retain the existing id
names (e.g., symbol-info
), and the corresponding template element will have an id
with the same name but a suffix of -template
(e.g., symbol-info-template
).
Using javascript, we can clone the template content into the page with the following code:
The rewrites
function will be used to modify the contents of the script so we can change the value of the symbol property in the widget options.
An example of a rewrite function would be:
and finally, the code to clone a specific template into the page would be:
We can repeat this process for all the other widgets (except the Ticker tape) as follows:
We are excluding Ticker Tape from this template cloning because it isn’t specific to a single symbol but rather is used to show the prices for multiple symbols.