0

I have a web site which inserts content via javascript's innerhtml function when a link is clicked (click it again and poof, the inserted data goes away). This works great. When I want to do now is add a flot plot to that injected code. The flot plot needs the javascript to run after it is inserted. Is this possible? The alternate would be a button in the inserted code to then run the JS... but that's not ideal. Thoughts? --Thanks!

Okay.. so here is some code: This first bit is the link that you click:

<a name = "<?php echo $tag_no ?>" class="none" href="#<?php echo $tag_no ?>" title = "<?php echo $tooltip ?>" onClick="sendRequest('GET','/2.0/details.php3?event_no=<?php echo $tag_no ?>&amp;details=0&amp;PHPSESSID=<?php echo session_id() ?>')"><?php echo $concert_date_formatted ?> </a></td>

It calls the javascript functions here:

function sendRequest(method, url){
    //alert(method);
    if(method == 'get' || method == 'GET'){
        http.open(method,url,true);
        http.onreadystatechange = function() {
            handleResponse(url,method);
        }
        http.send(null);
    } else if (method == "got" || method == "GOT"){
        handleResponse(url,method);
    }
}

function handleResponse(url,method){
    //alert("Junebug!");
    if(method == "GOT" || method == "got"){
        //document.getElementById(event_no).innerHTML = null;
        //alert("got");
        BetterInnerHTML(event_no,null);
    } else if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        var equals = url.indexOf('=');
        var andsign = url.indexOf('&');
        var event_no = url.substring(equals + 1, andsign);
        if(response){
            //alert(response);
            document.getElementById(event_no).innerHTML = response;
            //BetterInnerHTML(event_no,response);
        }
    } 

}

Which in turn insert the code.

Mark
  • 81
  • 8
  • Post some code is a good start if you want help – laaposto Feb 08 '14 at 20:53
  • AFAIK this won't work. Appending entire ` – Johannes H. Feb 08 '14 at 20:53
  • See [Executing – user2864740 Feb 08 '14 at 20:54
  • if you want help, help us and put your code. – Rodrigo Fonseca Feb 08 '14 at 21:00
  • 1
    Laaposto, Rodrigo, code is included above, thank you for reminding me that like myself you cannot read minds! Johannes can you elaborate on the method you suggest works? user2864740 I agree it may be a security hole that was plugged. Is that a particularly useful work around? – Mark Feb 09 '14 at 02:38
  • Johannes H. do you have an example that uses the method you noted as working? Thanks in advance! – Mark Feb 10 '14 at 23:25

1 Answers1

0

One way is to create an IFRAME inside that DIV and use document.write into that IFRAME:

document.getElementById(event_no).innerHTML = "<iframe></iframe>";
document.getElementById(event_no).firstChild.contentWindow.document.write(response);

Demo: http://jsfiddle.net/5B3Qs/

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • Yuriy... this seems to work... I am not familiar with iframes much... and the iframe appears small.. it should match the container width and length is defined by the length of the contents... can I make it do that? – Mark Feb 09 '14 at 05:00
  • You can manipulate IFRAME's size via width/height attributes. E.g. to make it fit the parent DIV try setting them to 100%: `""` See more IFRAME attributes at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe – Yuriy Galanter Feb 09 '14 at 05:28
  • ah cool, I should have checked before asking.. it can be modified in the same way as other html elements. I'll see what happens! – Mark Feb 09 '14 at 23:01
  • The iframe is cool and everything but it seems unable to get the height set to the contents. I have tried a variety of methods. Do you know of a way to set the height based on the incoming contents? – Mark Feb 10 '14 at 23:24
  • Do you really want iframe to auto-resize? Was this what the original DIV did? – Yuriy Galanter Feb 11 '14 at 18:05
  • The original innerhtml added the contents to the page without scroll bars, just extended the page by the half to three quarters of a page that the details it inserted were worth. The page was still flat in essence. – Mark Feb 11 '14 at 23:14
  • It may be getting overly complicated, you may want to rethink the design. Meanwhile take a look at this: http://codecorner.galanter.net/2009/09/30/resize-iframe-after-content-has-been-resized/ (it uses MS library, but doing the same in jQuery should be trivial) – Yuriy Galanter Feb 12 '14 at 15:41