Dynamic Javascript Blocks Evaluation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thinkfr33ly@hotmail.com

    Dynamic Javascript Blocks Evaluation

    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="JavaS cript" TYPE="text/JavaScript">
    var content = document.create Element("script ");
    content.src = "http://www.somedomain. com/Block_A.js";
    content.id = "blocka";
    content.languag e="javascript ";
    document.getEle mentsByTagName( "HEAD")[0].appendChild(co ntent);
    </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.docume ntElement.outer HTML);return false;"
    value="DOM Source" />
    </BODY>
    </HTML>
    --------------------------------------
    END Test.html
    --------------------------------------

    --------------------------------------
    START Block_A.js
    --------------------------------------
    document.write( "<SCRI"+"PT LANGUAGE='JavaS cript'
    SRC='http://www.somedomain. com/Block_B.js'></SCR"+"IPT>");
    alert("Block A Written");
    --------------------------------------
    END Block_A.js
    --------------------------------------

    --------------------------------------
    START Block_B.js
    --------------------------------------
    document.write( "<SCR"+"IPT LANGUAGE='Javas cript'>");
    document.write( "alert('Blo ck C Written');");
    document.write( "</SCR"+"IPT>");
    --------------------------------------
    END Block_B.js
    --------------------------------------

    Block C is directly written by Block B, so there is no external source
    for that.

  • Grant Wagner

    #2
    Re: Dynamic Javascript Blocks Evaluation

    <thinkfr33ly@ho tmail.com> wrote in message
    news:1106072225 .995733.324180@ c13g2000cwb.goo glegroups.com.. .[color=blue]
    >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.[/color]

    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 <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq


    Comment

    • RMD

      #3
      Re: Dynamic Javascript Blocks Evaluation

      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?

      Comment

      Working...