Dynamic Javascript Blocks Evaluation

Discussion in 'Javascript' started by thinkfr33ly, Jan 18, 2005.

  1. thinkfr33ly

    thinkfr33ly Guest

    I have a page that inserts a block of javascript dynamically into the
    page "Test.html". The inserted block, "Block A", then does a
    document.write of another script block "Block B". This script block
    does its own document.write of one last script block "Block C". The way
    this occurs is somewhat out of my control, and I need to find a way to
    work within this framework. This only needs to work in IE 5.5 or
    better.

    My problem is that the code in Block C is not evaluated by the
    javascript parser. I can get it to evaluate only by doing a "ctrl+n" in
    IE which causes IE to open the current document in a new window. This
    also happens to cause IE to re-parse the existing DOM's javascript and
    thereby finally execute Block C.

    I need a way to have Block C inserted and evaluated without having to
    do a CTRL+N. There are the following caveats to the modifacations I can
    make.

    1.) We can insert/modify whatever script we want in test.html. (With
    one caveat, see below.)
    2.) We have no ability to modify Block A's source.
    3.) We have no ability to modify Block B's source, except in that we
    can change the contents of its document.write calls.
    4.) We can modify whatever we want with Block C. (Since this block is
    just what's written by block C. See below.)

    The reason for these restrictions is that some of these scripts do not
    belong to us. (They belong to a 3rd party server suit and we cannot
    modify them.)

    The caveat concerning test.html is that whatever is written will be
    written *after* the page has been loaded into the browser. In fact, it
    will be written by manipulating the DOM with external code. (This stuff
    all lives in a client application hosting an IE control.) The insertion
    of the javascript occurs once the IE control says that the current
    document is completely done loading.

    As it is now, the client application is written to insert the
    javascript into either an existing javascript tag in the HEAD, or by
    creating a new javascript tag. We can change whatever we want as far as
    the contents, but not where it is inserted. Also, once the javascript
    is inserted, the client app executes a javascript function of our
    choice. Now, we can modify the client application if needed, but I
    would really rather avoid that if possible.

    Below is the contents of Test.html, as well as Block A, Block B, and
    Block C. Be sure to modify the SRC paths where needed.

    --------------------------------------
    START Test.html
    --------------------------------------
    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="JavaScript" TYPE="text/JavaScript">
    var content = document.createElement("script");
    content.src = "http://www.somedomain.com/Block_A.js";
    content.id = "blocka";
    content.language="javascript";
    document.getElementsByTagName("HEAD")[0].appendChild(content);
    </SCRIPT>
    </HEAD>
    <body>
    This is a test page.<br /><br />
    The first time it loads you will see <b>one dialog</b>.<br />
    Hit CTRL+N and you will now see <b>two dialogs</b>.<br />
    <br />
    Click the following button to view the current page DOM contents.
    <input type="submit"
    onclick="alert(document.documentElement.outerHTML);return false;"
    value="DOM Source" />
    </BODY>
    </HTML>
    --------------------------------------
    END Test.html
    --------------------------------------

    --------------------------------------
    START Block_A.js
    --------------------------------------
    document.write("<SCRI"+"PT LANGUAGE='JavaScript'
    SRC='http://www.somedomain.com/Block_B.js'></SCR"+"IPT>");
    alert("Block A Written");
     
    thinkfr33ly, Jan 18, 2005
    #1
    1. Advertisements

  2. thinkfr33ly

    Grant Wagner Guest

    Tested in:
    IE 6.0.2900: two alerts, one for Block A, one for Block C
    Firefox 1.0: two alerts, one for Block A, one for Block C
    Opera 7.54u1: two alerts, one for Block C, one for Block A (yes, in that
    order)

    So from my testing it seems to do what you want in IE. I did it all from
    my local hard disk, so it's possible it's a cross-domain security issue
    preventing the script from being executed.
     
    Grant Wagner, Jan 18, 2005
    #2
    1. Advertisements

  3. thinkfr33ly

    RMD Guest

    That's very strange that it would work on your machine. I've tested it
    both locally and remotely, and I've tested it on multiple machines
    (VMWare and real), and it fails on all.

    One thing I have noticed is that whether or not it works can be related
    to the speed of the machine. For instance, I can get it to work by
    placing an alert after the appendChild call in test.html. This blocks
    the execution of the rest of the page long enough for the script to get
    injected and evaluated.

    Unfourtunatly, this doesn't work in my actual setup because the script
    being shown as embedded in test.html is actually injected by the client
    application hosting the IE control long after the page has completely
    loaded.

    Any ideas?
     
    RMD, Jan 18, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.