Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > why is my global variable not being retained?

Reply
Thread Tools

why is my global variable not being retained?

 
 
carlos
Guest
Posts: n/a
 
      03-25-2008
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(sc ript);
}

function fillArrays(json) {

if(!postback) {

//store all entries
for (var i = 0; i < json.feed.entry.length; i++) {
var entry = json.feed.entry[i];
aTitle[i] = entry.title.$t;
var postDesc = "posted: ";
aDate[i] = postDesc + json.feed.updated.$t.substring(0,10) +
" ";
aContent[i] = 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[i];
document.getElementById('content').innerHTML = aContent[i];
document.getElementById('date').innerHTML = aDate[i];
}

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

document.getElementById('header').innerHTML = aTitle[i];
document.getElementById('content').innerHTML = aContent[i];
document.getElementById('date').innerHTML = aDate[i];
}




 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-25-2008
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(sc ript);


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>
 
Reply With Quote
 
 
 
 
lwhitb1
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 8:00 pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
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(sc ript);

>
> 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="javascriptage_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='javascriptage_load();'
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-25-2008
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_not...s.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="javascriptage_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
 
Reply With Quote
 
lwhitb1
Guest
Posts: n/a
 
      03-26-2008


>>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.

 
Reply With Quote
 
carlos
Guest
Posts: n/a
 
      03-26-2008
On Mar 25, 11:40 pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
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_not...s.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="javascriptage_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.
 
Reply With Quote
 
carlos
Guest
Posts: n/a
 
      03-26-2008
Check out the 2 links below. They do the same thing 2 different
ways.

http://www.brokerloanpost.com/bikebr/news2.htm

http://www.brokerloanpost.com/bikebr/news.htm

When you click next it should load the next array value's content, but
the page is reloading.

Thanks again
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-26-2008
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="javascriptage_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
 
Reply With Quote
 
lwhitb1
Guest
Posts: n/a
 
      03-27-2008
On Mar 26, 1:52*am, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
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="javascriptage_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?
 
Reply With Quote
 
carlos
Guest
Posts: n/a
 
      03-27-2008
On Mar 26, 1:52*am, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
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="javascriptage_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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is it not possible to assign a global variable to anotherglobal variable..? Jase Schick C Programming 11 08-05-2010 05:16 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Global variable not being set kevinold@gmail.com Javascript 6 06-16-2006 01:34 AM



Advertisments