Javascript innerHTML

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

    Javascript innerHTML

    How can I get the innerHTML of a <div> area only when the page loads, then use that variable in a function?

    Here is my code:
    function setContent(zz)
    {
    var lb = document.getEle mentById('leftb ar').innerHTML;
    var rb = document.getEle mentById('right bar').innerHTML ;
    document.getEle mentById("myCon tent").innerHTM L = "<span class=\"title_P age\">"+Page[zz]+"</span>";
    if (zz=="home") {
    document.getEle mentById('leftb ar').innerHTML = lb;
    document.getEle mentById('right bar').innerHTML = rb;
    document.getEle mentById('leftb ar').style.widt h = '135px';
    document.getEle mentById('right bar').style.wid th = '135px';
    }
    else {
    document.getEle mentById('leftb ar').innerHTML = "";
    document.getEle mentById('right bar').innerHTML = "";
    document.getEle mentById('leftb ar').style.widt h = '0px';
    document.getEle mentById('right bar').style.wid th = '0px';
    }
    }

    I want lb and rb to be set only once (i.e. only when the page loads the first time). I tried putting those variables outside the function, but when I do they, they show up as undefined. So how could i code it so that when the page loads, it assigns the innerHTML to the two variables, but not any other time while that page is open?

    Chris

    --
    Composed with Newz Crawler 1.4 http://www.newzcrawler.com/
  • HikksNotAtHome

    #2
    Re: Javascript innerHTML

    In article <z06cb.23390$JM .10393970@news4 .srv.hcvlny.cv. net>, "Chris"
    <nyk52687@yahoo .com> writes:
    [color=blue]
    >How can I get the innerHTML of a <div> area only when the page loads, then
    >use that variable in a function?
    >
    >Here is my code:
    >function setContent(zz)
    >{
    > var lb = document.getEle mentById('leftb ar').innerHTML;
    > var rb = document.getEle mentById('right bar').innerHTML ;[/color]

    Remove the above two lines from the setContent function.

    Use the bodys onload even to trigger a function that will set the global
    variables to what you want.

    var lb,rb;
    function pageLoaded(){
    lb = document.getEle mentById('leftb ar').innerHTML;
    rb = document.getEle mentById('right bar').innerHTML ;
    }
    <body onload="pageLoa ded()">
    Now, you can use the global lb and rb in your second function.
    --
    Randy

    Comment

    • Chris

      #3
      Re: Re: Javascript innerHTML

      > In article <z06cb.23390$JM .10393970@news4 .srv.hcvlny.cv. net>, "Chris"[color=blue]
      > <nyk52687@yahoo .com> writes:
      >[color=green]
      > >How can I get the innerHTML of a <div> area only when the page loads, then
      > >use that variable in a function?
      > >
      > >Here is my code:
      > >function setContent(zz)
      > >{
      > > var lb = document.getEle mentById('leftb ar').innerHTML;
      > > var rb = document.getEle mentById('right bar').innerHTML ;[/color]
      >
      > Remove the above two lines from the setContent function.
      >
      > Use the bodys onload even to trigger a function that will set the global
      > variables to what you want.
      >
      > var lb,rb;
      > function pageLoaded(){
      > lb = document.getEle mentById('leftb ar').innerHTML;
      > rb = document.getEle mentById('right bar').innerHTML ;
      > }
      > <body onload="pageLoa ded()">
      > Now, you can use the global lb and rb in your second function.
      > --
      > Randy[/color]
      When I do that, both variables show undefined.

      Chris

      --
      Composed with Newz Crawler 1.4 http://www.newzcrawler.com/

      Comment

      • HikksNotAtHome

        #4
        Re: Javascript innerHTML

        In article <EIecb.1683$XF. 748596@news4.sr v.hcvlny.cv.net >, "Chris"
        <nyk52687@yahoo .com> writes:

        <snip>
        [color=blue]
        >When I do that, both variables show undefined.[/color]

        Test page online? I tested it in IE6 just to confirm my beliefs.

        var someVar = "Not loaded";

        function checkFunction() {
        alert(someVar)
        }
        function pageLoaded(){
        someVar = "Loaded";
        }

        <body onload="pageLoa ded()">

        <button onclick="checkF unction()">Chec k the Var</button>

        The alert I get is "Loaded". Sounds like you are trying to call something
        before it exists, hence the request to see a test page.
        --
        Randy

        Comment

        • Chris

          #5
          Re: Re: Javascript innerHTML

          > In article <EIecb.1683$XF. 748596@news4.sr v.hcvlny.cv.net >, "Chris"[color=blue]
          > <nyk52687@yahoo .com> writes:
          >
          > <snip>
          >[color=green]
          > >When I do that, both variables show undefined.[/color]
          >
          > Test page online? I tested it in IE6 just to confirm my beliefs.
          >
          > var someVar = "Not loaded";
          >
          > function checkFunction() {
          > alert(someVar)
          > }
          > function pageLoaded(){
          > someVar = "Loaded";
          > }
          >
          > <body onload="pageLoa ded()">
          >
          > <button onclick="checkF unction()">Chec k the Var</button>
          >
          > The alert I get is "Loaded". Sounds like you are trying to call something
          > before it exists, hence the request to see a test page.
          > --
          > Randy[/color]
          I've tested it on IIS. As for calling something before it exists, would you recommend I put the function in the page header, an external js file, or somehwere in the <body>?

          Thanks, Chris

          --
          Composed with Newz Crawler 1.4 http://www.newzcrawler.com/

          Comment

          Working...