why is my global variable not being retained?

Discussion in 'Javascript' started by carlos, Mar 25, 2008.

  1. carlos

    carlos Guest

    I am writing the below javascript to populate an array with content
    that I get from a google blog feed. I load the first array items value
    by doing an innerHTML on a DOM element. The problem I am facing is
    that when a user clicks the next button, I want my javascript function
    (showNext) to get the next array item, and inject that array value in
    the dom element. It seems to be working, but after I inject the new
    content, the page is being reloaded, and it calls the same method that
    originally loaded the first array items content (page_load - which is
    called on the body onload). I tried setting a global variable called
    "postback" that set the value to true once the page loads for the
    first time. However, after I click next, the value gets reset again. I
    am confused as to why this value is not being retained? It is in the
    global cache right? Can someone take a peak at the code and give me
    some suggestions?

    var aContent = new Array();
    var aTitle = new Array();
    var aDate = new Array();
    var activeIndex = 0;
    var postBack = false;

    function page_load(query) {

    // Retrieve the JSON feed.
    var script = document.createElement('script');
    script.setAttribute('src', 'http://' + 'bikebr' + '.blogspot.com/
    feeds/posts' +
    '/default?alt=json-in-
    script&callback=fillArrays');
    script.setAttribute('id', 'jsonScript');
    script.setAttribute('type', 'text/javascript');
    document.documentElement.firstChild.appendChild(script);
    }

    function fillArrays(json) {

    if(!postback) {

    //store all entries
    for (var i = 0; i < json.feed.entry.length; i++) {
    var entry = json.feed.entry;
    aTitle = entry.title.$t;
    var postDesc = "posted: ";
    aDate = postDesc + json.feed.updated.$t.substring(0,10) +
    " ";
    aContent = entry.content.$t;
    }

    //inject the first blog items into the page
    document.getElementById('content').innerHTML = aContent[0];
    document.getElementById('header').innerHTML = aTitle[0];
    document.getElementById('date').innerHTML = aDate[0];
    postBack = true;
    }
    }

    function showContent(data) {
    var divToInject = document.getElementById('data');
    var newDiv = document.createElement('div');
    newDiv.innerHTML = data;
    divToInject.appendChild(newDiv);
    }

    function showNext() {

    var i = activeIndex + 1;
    if(i > aContent.length) {
    i==0;
    }
    debugger;
    document.getElementById('header').innerHTML = aTitle;
    document.getElementById('content').innerHTML = aContent;
    document.getElementById('date').innerHTML = aDate;
    }

    function showPrev() {
    var i = activeIndex - 1;
    if(i <= 0) {
    i==0;
    }

    document.getElementById('header').innerHTML = aTitle;
    document.getElementById('content').innerHTML = aContent;
    document.getElementById('date').innerHTML = aDate;
    }
    carlos, Mar 25, 2008
    #1
    1. Advertising

  2. carlos wrote:
    > I am writing the below javascript to populate an array with content
    > that I get from a google blog feed. I load the first array items value
    > by doing an innerHTML on a DOM element. The problem I am facing is
    > that when a user clicks the next button, I want my javascript function
    > (showNext) to get the next array item, and inject that array value in
    > the dom element. It seems to be working, but after I inject the new
    > content, the page is being reloaded,


    There is your problem. You have to prevent the reload.

    > [...] However, after I click next, the value gets reset again. I
    > am confused as to why this value is not being retained? It is in the
    > global cache right?


    Wrong. A global variable is a property of the Global Object with the
    DontDelete attribute. The Global Object and its associated execution
    context is destructed when the document resource is reloaded; property
    values are _not_ stored in the browser cache.

    > Can someone take a peak at the code and give me some suggestions?


    You should post how you call these methods.

    > [...]
    > function page_load(query) {
    >
    > // Retrieve the JSON feed.
    > var script = document.createElement('script');
    > script.setAttribute('src', 'http://' + 'bikebr' + '.blogspot.com/
    > feeds/posts' +
    > '/default?alt=json-in-
    > script&callback=fillArrays');
    > script.setAttribute('id', 'jsonScript');
    > script.setAttribute('type', 'text/javascript');


    setAttribute() is unnecessary here.

    script.src = "...";
    script.id = "...";
    script.type = "...";

    > document.documentElement.firstChild.appendChild(script);


    You would want to append the `script' element as the child element of the
    `head' element, which is not necessarily the first child of the `html' root
    element: text nodes are child nodes, too. Consider this:

    document.getElementsByTagName("head")[0].appendChild(script);

    Loading scripts either way is no bullet-proof method, though. And to spare
    you looking for one: there is none but to write the `script' element as-is.


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
    Thomas 'PointedEars' Lahn, Mar 25, 2008
    #2
    1. Advertising

  3. carlos

    lwhitb1 Guest

    On Mar 25, 8:00 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > carlos wrote:
    > > I am writing the below javascript to populate an array with content
    > > that I get from a google blog feed. I load the first array items value
    > > by doing an innerHTML on a DOM element. The problem I am facing is
    > > that when a user clicks the next button, I want my javascript function
    > > (showNext) to get the next array item, and inject that array value in
    > > the dom element. It seems to be working, but after I inject the new
    > > content, the page is being reloaded,

    >
    > There is your problem. You have to prevent the reload.
    >
    > > [...] However, after I click next, the value gets reset again. I
    > > am confused as to why this value is not being retained? It is in the
    > > global cache right?

    >
    > Wrong. A global variable is a property of the Global Object with the
    > DontDelete attribute. The Global Object and its associated execution
    > context is destructed when the document resource is reloaded; property
    > values are _not_ stored in the browser cache.
    >
    > > Can someone take a peak at the code and give me some suggestions?

    >
    > You should post how you call these methods.
    >
    > > [...]
    > > function page_load(query) {

    >
    > > // Retrieve the JSON feed.
    > > var script = document.createElement('script');
    > > script.setAttribute('src', 'http://' + 'bikebr' + '.blogspot.com/
    > > feeds/posts' +
    > > '/default?alt=json-in-
    > > script&callback=fillArrays');
    > > script.setAttribute('id', 'jsonScript');
    > > script.setAttribute('type', 'text/javascript');

    >
    > setAttribute() is unnecessary here.
    >
    > script.src = "...";
    > script.id = "...";
    > script.type = "...";
    >
    > > document.documentElement.firstChild.appendChild(script);

    >
    > You would want to append the `script' element as the child element of the
    > `head' element, which is not necessarily the first child of the `html' root
    > element: text nodes are child nodes, too. Consider this:
    >
    > document.getElementsByTagName("head")[0].appendChild(script);
    >
    > Loading scripts either way is no bullet-proof method, though. And to spare
    > you looking for one: there is none but to write the `script' element as-is.
    >
    > PointedEars
    > --
    > Use any version of Microsoft Frontpage to create your site.
    > (This won't prevent people from viewing your source, but no one
    > will want to steal it.)
    > -- from <http://www.vortex-webdesign.com/help/hidesource.htm>


    Thanks for the feedback.

    I call the page_load function in the body onload tag like below: It
    doesn't require a parameter. I should have removed the query parameter
    in the previous post.

    body onload="javascript:page_load();"

    Do you have any suggestions as to how I can prevent the page from
    reloading, or from preventing the destruction of the global variable?

    Thanks

    <body onload='javascript:page_load();'
    lwhitb1, Mar 25, 2008
    #3
  4. lwhitb1 wrote:
    > [...] Thomas 'PointedEars' Lahn [...] wrote:
    >> carlos wrote:
    >>> I am writing the below javascript to populate an array with content
    >>> that I get from a google blog feed. I load the first array items value
    >>> by doing an innerHTML on a DOM element. The problem I am facing is
    >>> that when a user clicks the next button, I want my javascript function
    >>> (showNext) to get the next array item, and inject that array value in
    >>> the dom element. It seems to be working, but after I inject the new
    >>> content, the page is being reloaded,

    >> There is your problem. You have to prevent the reload.
    >> [...]
    >>> Can someone take a peak at the code and give me some suggestions?

    >>
    >> You should post how you call these methods.
    >> [...]

    >
    > Thanks for the feedback.


    You are welcome. Please trim your quotes to the minimum required to retain
    context: http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Trim

    It would also be appropriate if you chose only one pseudonym to post,
    although a real name would be appreciated even more.
    http://rfc-editor.org/rfc/rfc1855.txt

    > I call the page_load function in the body onload tag like below: It
    > doesn't require a parameter. I should have removed the query parameter
    > in the previous post.
    >
    > body onload="javascript:page_load();"


    Then that is pointless. No parameter means static code:

    <script type="text/javascript"
    src="http://bikebr.blogspot.com/feeds/posts"></script>

    And `javascript:' is a URI scheme that does not belong into event handler
    attributes where it only serves as a label, if that.

    > Do you have any suggestions as to how I can prevent the page from
    > reloading, or from preventing the destruction of the global variable?


    If you posted the *calling* code, I might have some.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Mar 25, 2008
    #4
  5. carlos

    lwhitb1 Guest


    >>Then that is pointless. No parameter means static code:


    Actually, it is not static, because in that src url, a callback method
    is given, and the JSON feed is passed to that method.

    <script type="text/javascript" src="http://bikebrblogspot.com/feeds/
    posts/default?alt=json-in-script&callback=fillArrays";></script>

    Thanks for the feedback. I will take a look at the links you provided.
    Also, i'll try to upload my code on a server so you can review it.
    lwhitb1, Mar 26, 2008
    #5
  6. carlos

    carlos Guest

    On Mar 25, 11:40 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > lwhitb1 wrote:
    > > [...] Thomas 'PointedEars' Lahn [...] wrote:
    > >> carlos wrote:
    > >>> I am writing the below javascript to populate an array with content
    > >>> that I get from a google blog feed. I load the first array items value
    > >>> by doing an innerHTML on a DOM element. The problem I am facing is
    > >>> that when a user clicks the next button, I want my javascript function
    > >>> (showNext) to get the next array item, and inject that array value in
    > >>> the dom element. It seems to be working, but after I inject the new
    > >>> content, the page is being reloaded,
    > >> There is your problem. You have to prevent the reload.
    > >> [...]
    > >>> Can someone take a peak at the code and give me some suggestions?

    >
    > >> You should post how you call these methods.
    > >> [...]

    >
    > > Thanks for the feedback.

    >
    > You are welcome. Please trim your quotes to the minimum required to retain
    > context:http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Trim
    >
    > It would also be appropriate if you chose only one pseudonym to post,
    > although a real name would be appreciated even more.http://rfc-editor.org/rfc/rfc1855.txt
    >
    > > I call the page_load function in the body onload tag like below: It
    > > doesn't require a parameter. I should have removed the query parameter
    > > in the previous post.

    >
    > > body onload="javascript:page_load();"

    >
    > Then that is pointless. No parameter means static code:
    >
    > <script type="text/javascript"
    > src="http://bikebr.blogspot.com/feeds/posts"></script>
    >
    > And `javascript:' is a URI scheme that does not belong into event handler
    > attributes where it only serves as a label, if that.
    >
    > > Do you have any suggestions as to how I can prevent the page from
    > > reloading, or from preventing the destruction of the global variable?

    >
    > If you posted the *calling* code, I might have some.
    >
    > PointedEars
    > --
    > var bugRiddenCrashPronePieceOfJunk = (
    > navigator.userAgent.indexOf('MSIE 5') != -1
    > && navigator.userAgent.indexOf('Mac') != -1
    > ) // Plone, register_function.js:16


    >>Then that is pointless. No parameter means static code:


    Actually, it is not static, because in that src url, a callback method
    is given, and the JSON feed is passed to that method.

    <script type="text/javascript" src="http://bikebrblogspot.com/feeds/
    posts/default?alt=json-in-script&callback=fillArrays";></script>

    Thanks for the feedback. I will take a look at the links you provided.
    Also, i'll upload my code on a server so you can review it.
    carlos, Mar 26, 2008
    #6
  7. carlos

    carlos Guest

    carlos, Mar 26, 2008
    #7
  8. carlos wrote:
    > [...] Thomas 'PointedEars' Lahn [...] wrote:
    >> lwhitb1 wrote:
    >> [...]
    >>> I call the page_load function in the body onload tag like below: It
    >>> doesn't require a parameter. I should have removed the query parameter
    >>> in the previous post.
    >>> body onload="javascript:page_load();"

    >> [...]


    Again, please quote properly. There will be no further requests.

    [Fixed quotation levels]
    >> Then that is pointless. No parameter means static code:

    >
    > Actually, it is not static, because in that src url, a callback method
    > is given, and the JSON feed is passed to that method.
    >
    > <script type="text/javascript" src="http://bikebrblogspot.com/feeds/
    > posts/default?alt=json-in-script&callback=fillArrays";></script>


    That is static *HTML code*, yes? And invalid, must be:

    <script type="text/javascript"
    src="http://bikebrblogspot.com/feeds/posts/default?alt=json-in-script&amp;callback=fillArrays"></script>

    See http://validator.w3.org/

    > Thanks for the feedback. I will take a look at the links you provided.
    > Also, i'll upload my code on a server so you can review it.


    ACK.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Mar 26, 2008
    #8
  9. carlos

    lwhitb1 Guest

    On Mar 26, 1:52 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > carlos wrote:
    > > [...] Thomas 'PointedEars' Lahn [...] wrote:
    > >> lwhitb1 wrote:
    > >> [...]
    > >>> I call the page_load function in the body onload tag like below: It
    > >>> doesn't require a parameter. I should have removed the query parameter
    > >>> in the previous post.
    > >>> body onload="javascript:page_load();"
    > >> [...]

    >
    > Again, please quote properly.  There will be no further requests.
    >
    > [Fixed quotation levels]
    >
    > >> Then that is pointless.  No parameter means static code:

    >
    > > Actually, it is not static, because in that src url, a callback method
    > > is given, and the JSON feed is passed to that method.

    >
    > > <script type="text/javascript" src="http://bikebrblogspot.com/feeds/
    > > posts/default?alt=json-in-script&callback=fillArrays";></script>

    >
    > That is static *HTML code*, yes?  And invalid, must be:
    >
    >   <script type="text/javascript"
    > src="http://bikebrblogspot.com/feeds/posts/default?alt=json-in-script&...."></script>
    >
    > Seehttp://validator.w3.org/
    >
    > > Thanks for the feedback. I will take a look at the links you provided.
    > > Also, i'll upload my code on a server so you can review it.

    >
    > ACK.
    >
    > PointedEars
    > --
    >     realism:    HTML 4.01 Strict
    >     evangelism: XHTML 1.0 Strict
    >     madness:    XHTML 1.1 as application/xhtml+xml
    >                                                     -- Bjoern Hoehrmann


    What makes that code static? Do tell. Are my quotes ok for you now?
    lwhitb1, Mar 27, 2008
    #9
  10. carlos

    carlos Guest

    On Mar 26, 1:52 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > carlos wrote:
    > > [...] Thomas 'PointedEars' Lahn [...] wrote:
    > >> lwhitb1 wrote:
    > >> [...]
    > >>> I call the page_load function in the body onload tag like below: It
    > >>> doesn't require a parameter. I should have removed the query parameter
    > >>> in the previous post.
    > >>> body onload="javascript:page_load();"
    > >> [...]

    >
    > Again, please quote properly.  There will be no further requests.
    >
    > [Fixed quotation levels]
    >
    > >> Then that is pointless.  No parameter means static code:

    >
    > > Actually, it is not static, because in that src url, a callback method
    > > is given, and the JSON feed is passed to that method.

    >
    > > <script type="text/javascript" src="http://bikebrblogspot.com/feeds/
    > > posts/default?alt=json-in-script&callback=fillArrays";></script>

    >
    > That is static *HTML code*, yes?  And invalid, must be:
    >
    >   <script type="text/javascript"
    > src="http://bikebrblogspot.com/feeds/posts/default?alt=json-in-script&...."></script>
    >
    > Seehttp://validator.w3.org/
    >
    > > Thanks for the feedback. I will take a look at the links you provided.
    > > Also, i'll upload my code on a server so you can review it.

    >
    > ACK.
    >
    > PointedEars
    > --
    >     realism:    HTML 4.01 Strict
    >     evangelism: XHTML 1.0 Strict
    >     madness:    XHTML 1.1 as application/xhtml+xml
    >                                                     -- Bjoern Hoehrmann


    Ok.. It is rather obvious that your trying to convey that I'm not
    quoting properly, and that I have invalid javascript. Do you believe
    that the page is not refreshing as a result of the invalid javascript?
    carlos, Mar 27, 2008
    #10
  11. carlos

    pr Guest

    carlos wrote:
    > Ok.. It is rather obvious that your trying to convey that I'm not
    > quoting properly, and that I have invalid javascript. Do you believe
    > that the page is not refreshing as a result of the invalid javascript?


    You are not quoting the minimum that makes sense of your post without
    the need to look back over previous posts. The fact that you appear to
    be two different people with a propensity for posting in tandem isn't
    winning you any marks either.

    Looking at the URLs you provided earlier, it looks like your trouble is
    simply that you are following the link

    <a href="" onclick="showNext();">Next Item</a>

    instead of cancelling it in the showNext() function. A more practical
    approach is

    <a href="server_equivalent.php"
    onclick="return showNext();">Next Item</a>

    and

    function showNext() {
    ...

    return false;
    }

    So the link isn't followed where JavaScript is available but is followed
    (to a sensible outcome) in other cases.
    pr, Mar 27, 2008
    #11
  12. carlos

    carlos Guest

    On Mar 27, 5:36 am, pr <> wrote:
    > carlos wrote:
    > > Ok.. It is rather obvious that your trying to convey that I'm not
    > > quoting properly, and that I have invalid javascript. Do you believe
    > > that the page is not refreshing as a result of the invalid javascript?

    >
    > You are not quoting the minimum that makes sense of your post without
    > the need to look back over previous posts. The fact that you appear to
    > be two different people with a propensity for posting in tandem isn't
    > winning you any marks either.
    >
    > Looking at the URLs you provided earlier, it looks like your trouble is
    > simply that you are following the link
    >
    >    <a href="" onclick="showNext();">Next Item</a>
    >
    > instead of cancelling it in the showNext() function. A more practical
    > approach is
    >
    >    <a href="server_equivalent.php"
    >        onclick="return showNext();">Next Item</a>
    >
    > and
    >
    >    function showNext() {
    >      ...
    >
    >      return false;
    >    }
    >
    > So the link isn't followed where JavaScript is available but is followed
    > (to a sensible outcome) in other cases.


    I appreciate all the help. My apologies for the quotes. I'll be sure
    to keep that in mind from now on.

    Thanks
    carlos, Mar 27, 2008
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve - DND
    Replies:
    5
    Views:
    1,729
    Alvin Bruney [MVP]
    Apr 25, 2004
  2. Chris Mahoney

    Sessions not retained in Firefox/Safari

    Chris Mahoney, Sep 13, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,304
    aatif
    Aug 15, 2006
  3. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    why is ddl.selectedValue not retained in viewstate ?

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Oct 3, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    480
    JIMCO Software
    Oct 4, 2005
  4. Replies:
    1
    Views:
    362
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page