innerHTML and variable scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick

    innerHTML and variable scope

    OK. I've had a trawl through past messages but can see nothing that
    helps me, so...here goes...

    I am writing a web application which, among other things, pushes a web
    page to a user's browser.

    In very simple terms:

    - I have a script in hidden frame A which sets the document.locati on
    of main frame B.

    - Once the document has been loaded in frame B, the script updates the
    document.body.i nnerHTML of that document.

    (Pushing the URL alone is not enough, since I want to push values in
    form fields etc)

    This model works great if I am pushing simple web pages, but as soon
    as the page I am pushing has complex javascript in it, bad things
    start happening.

    One such page has a load of js file includes, which are used for the
    drop down menus. Loaded normally in the browser, all the js on this
    page works fine. However, if the page has been pushed to the browser
    using my system, I get lots of '(varname) is null or not an object'
    errors when I try to use the menus.

    Now this smacks of a var scope problem, or somesuch. But I have no
    real idea.

    Can anyone suggest what might be happening here, or has anyone come
    across this problem before?

    I can post up some code if you want, but I thought I'd just get a
    general idea if anyone has come across this problem first.

    Cheers in advance for any ideas thrown into the ring!
  • Randy Webb

    #2
    Re: innerHTML and variable scope

    Nick wrote:

    <--snip-->
    [color=blue]
    > - Once the document has been loaded in frame B, the script updates the
    > document.body.i nnerHTML of that document.
    >[/color]

    <--snip-->
    [color=blue]
    > This model works great if I am pushing simple web pages, but as soon
    > as the page I am pushing has complex javascript in it, bad things
    > start happening.[/color]

    <--snip-->
    [color=blue]
    > Can anyone suggest what might be happening here, or has anyone come
    > across this problem before?[/color]

    Your problem is not one of scope, but of execution. If you "push" this code:

    <script type="text/javascript">
    alert('It executed');
    </script>

    You won't get the alert because it doesn't get executed.

    Some possibilities:

    document.write the frame instead of innerHTML.
    Use createElement to create a script element and then append (as a
    child), the data, or a src="someFile.j s" attribute.


    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq/

    Comment

    • Nick

      #3
      Re: innerHTML and variable scope

      > Your problem is not one of scope, but of execution. If you "push" this code:[color=blue]
      >
      > <script type="text/javascript">
      > alert('It executed');
      > </script>
      >
      > You won't get the alert because it doesn't get executed.
      >
      > Some possibilities:
      >
      > document.write the frame instead of innerHTML.
      > Use createElement to create a script element and then append (as a
      > child), the data, or a src="someFile.j s" attribute.[/color]

      OK Randy, I was doing some thinking last night, and thought I'd try
      chasing up document.write as well. As an experiment, I took the source
      code of my problematic page with pulldown menus, stuck it all in a
      textarea, and wrote a very short script to document.write the contents
      of that field out to another frame.

      top.frames['two'].document.open( "about:blan k");
      top.frames['two'].document.write ln(document.daf orm.sourcecode. value);
      top.frames['two'].document.close ();

      But it doesn't work! Again the problem is with the javascript in the
      page being pushed. In the main body of this document, there is an
      inline call to a js function which is defined in an external file
      referenced in the HEAD. I get an "object expected" error meaning that
      it can't find the function called on that line.

      I'm guessing that when a document is built using document.write( ) the
      different parts of the page - inline js, external js etc - are
      executed in a different order.

      Funnily enough, when I refresh the page that has been pushed, it WORKS
      ABSOLUTELY FINE! So obviously the browser has built the page in the
      right order on refresh.

      Oh yeah: I tried splitting the source code into lines (using "split"
      on newlines), and then writing out each line separately. I thought if
      I wrote each line separately, it might affect the "parse order".

      IE crashed after a few lines :(

      Thanks for your other suggestions, too, but I feel that if I could get
      either the document.locati on/innerHTML or document.write( ) models
      working, that would be the best course.

      Any further suggestions?

      Cheers for your continuing help.

      Nick

      Comment

      • Randy Webb

        #4
        Re: innerHTML and variable scope

        Nick wrote:[color=blue][color=green]
        >>Your problem is not one of scope, but of execution. If you "push" this code:
        >>
        >><script type="text/javascript">
        >>alert('It executed');
        >></script>
        >>
        >>You won't get the alert because it doesn't get executed.
        >>
        >>Some possibilities:
        >>
        >>document.writ e the frame instead of innerHTML.
        >>Use createElement to create a script element and then append (as a
        >>child), the data, or a src="someFile.j s" attribute.[/color]
        >
        >
        > OK Randy, I was doing some thinking last night, and thought I'd try
        > chasing up document.write as well. As an experiment, I took the source
        > code of my problematic page with pulldown menus, stuck it all in a
        > textarea, and wrote a very short script to document.write the contents
        > of that field out to another frame.
        >
        > top.frames['two'].document.open( "about:blan k");
        > top.frames['two'].document.write ln(document.daf orm.sourcecode. value);
        > top.frames['two'].document.close ();
        >
        > But it doesn't work! Again the problem is with the javascript in the
        > page being pushed. In the main body of this document, there is an
        > inline call to a js function which is defined in an external file
        > referenced in the HEAD. I get an "object expected" error meaning that
        > it can't find the function called on that line.[/color]

        The first test page I made is at:


        It uses a text area to get the code, and document.write' s it to the
        right hand frame. The code that is default in the textarea was the code
        I was testing it with. It loads an external .js file and then executes
        it onclick of the link it generates. So I am not sure why yours is not
        working. The only major difference I see between mine and yours is that
        I am not using .open('about:bl ank') but when I used it, it didn't seem
        to make any difference.

        [color=blue]
        > I'm guessing that when a document is built using document.write( ) the
        > different parts of the page - inline js, external js etc - are
        > executed in a different order.[/color]

        No. When the page is loaded, it is parsed in the order that you give it
        unless you use a defer attribute (which I believe to be an IE-only
        attribute, not sure because I never use it).
        [color=blue]
        > Funnily enough, when I refresh the page that has been pushed, it WORKS
        > ABSOLUTELY FINE! So obviously the browser has built the page in the
        > right order on refresh.
        >
        > Oh yeah: I tried splitting the source code into lines (using "split"
        > on newlines), and then writing out each line separately. I thought if
        > I wrote each line separately, it might affect the "parse order".
        >
        > IE crashed after a few lines :([/color]

        Splitting the source shouldn't matter, but it crashing IE could be a
        side effect of it since its introducing more and more calls to
        document.write. BTW, what version IE are you using?
        [color=blue]
        > Thanks for your other suggestions, too, but I feel that if I could get
        > either the document.locati on/innerHTML or document.write( ) models
        > working, that would be the best course.
        >
        > Any further suggestions?[/color]

        Are frames a requirement? It would be easier to "push" innerHTML into a
        div tag and load the external .js files in the main page. Thereby
        eliminating the problem. You could try that approach with the frames but
        the external files would have to be re-written to handle the
        cross-frames issues.


        --
        Randy
        Chance Favors The Prepared Mind
        comp.lang.javas cript FAQ - http://jibbering.com/faq/

        Comment

        • Woody

          #5
          Re: innerHTML and variable scope

          hmm, i built page in js, worked great withteh includes then put it into
          asp.. same problem as you, so i hardcopied teh included stuff into teh
          page and now it works


          Woody
          any sugestion or comment made by me should be examined first for
          validity and appropriateness before assuming i have any idea at all
          what the heck i am talking about. I am not responsible for anything you
          may see with my name attached to it, i think.

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          Working...