Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all.
I am trying to figure out a way to embed a code in my plain html page. It takes the code from the user for example

XML
<!-- TradingView Chart BEGIN -->
<script type="text/javascript" src="https://s3.amazonaws.com/tradingview/tv.js"></script>
<script type="text/javascript">
var tradingview_embed_options = {};
tradingview_embed_options.width = 640;
tradingview_embed_options.height = 400;
tradingview_embed_options.chart = 'vkcQRj1Z';
new TradingView.chart(tradingview_embed_options);
</script>
<p><a href="https://www.tradingview.com/chart/XOM/vkcQRj1Z-Consolidate/">Consolidate</a> by <a href="https://www.tradingview.com/u/lvn2007/">lvn2007</a> on <a href="https://www.tradingview.com/">TradingView.com</a></p>
<!-- TradingView Chart END -->


and inject it into my webpage, to show the chart which this code represent as you can see its in javascript, so i am looking for a solution where i don't have to refresh or go to the new page to trigger this javascript, which right now is not working.
Thanks.
Posted

I have no idea why would you use some Amazon library (or whatever it is; you can ask them how to use it), but the problem of "embedding" any HTML code in the current page is ridiculously simple. Try, for example, this:
JavaScript
document.body.innerHTML = "<h1>Write here any content you want!</h1>

Naturally, you can "embed" anything in any given HTML element. If you don't know how to locate the element you need, you can use document.getElementById, in particular. To transform HTML instead of just writing it, you can read the value of the innerHTML property, analyze it and write a new value. So, ultimately, you can write anything your want.

There are many more fine-grain method of transforming your document DOM, but I would rather recommend jQuery: http://api.jquery.com/category/manipulation[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery,
http://jquery.com,
http://learn.jquery.com,
http://learn.jquery.com/using-jquery-core,
http://learn.jquery.com/about-jquery/how-jquery-works (start from here).

—SA
 
Share this answer
 
Comments
SohaibX 1-Jun-15 8:45am    
Thanks. But I know how to inject static content and html tags into the document via javascript, but as you can see in my question its not the plain tags, its the javascript which i have to inject in code, and it should also return be resuls, which would be a chart, without postback of the page
Sergey Alexandrovich Kryukov 1-Jun-15 11:23am    
Can you be logical? The whole HTML is the tree of "plain tags", with text nodes and element attributes, and nothing else. You can "inject" absolutely everything in any element, any thinkable HTML. You can even "inject" a script tag (which is also HTML!) and immediately call functions of new JavaScript code. Do whatever you want (and face the consequences).
—SA
HI.

Here is your solution.

I did following changes to your code

1). Add reference to jquery library.

2). Save a reference chart class
var c1=new TradingView.chart(tradingview_embed_options);

3). Call render method
c1.render();

4). Remove extra link using jquery

$("div.tradingview-widget a").remove();


Changed code is as under.

$(document).ready(function()
{
var tradingview_embed_options = {};
tradingview_embed_options.width = 640;
tradingview_embed_options.height = 400;
tradingview_embed_options.chart = 'vkcQRj1Z';
var c1=new TradingView.chart(tradingview_embed_options);
c1.render();

$("div.tradingview-widget a").remove(); });

Below is Jsfiddle link

https://jsfiddle.net/0tnm1bar/1/[^]
 
Share this answer
 
v2
Comments
SohaibX 1-Jun-15 7:22am    
Thanks. but i know How to trigger the js in document.ready. let me rephrase my question.
User click Embed Button, a Dialogue box opens, user paste his embedded code there. now i can simple inject that code anywhere in the page, but the problem is it did not show the chart, probably because the javascript never loaded or something and I don't want to postback the page.
Any help will be appreciated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900