By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,328 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

Javascript looses mind on resize event

P: n/a
I am having a problem that is driving me crazy and I am whits end. I am
hoping that someone here can help me. I have a client server application
where the client is drawing a progressive bar graph based on data
retrieved from the server once a second. The basic loop looks like this;

</HEAD>
<script language="JavaScript1.2">

var nodedatareq;
function buildGraphs() {

document.writeln("<BODY>");

// build graphs and add them

document.writeln("</BODY>");

window.addEventListener("resize",resizeEventHandle r, false);
}

function initXMLHTTPRequest() {
var xreq= null
if( window.XMLHttpRequest ) {
xreq= new XMLHttpRequest()
} else if(window.ActiveXObject) {
xreq= new ActiveXObject("Microsoft.XMLHTTP")
}
return xreq
}

function sendNodeRequest()
{
var mnodedataod="POST";
nodedatareq = initXMLHTTPRequest();
if( nodedatareq ) {
nodedataParam = "nodedata="+ escape(1)
nodedatareq.onreadystatechange=nodedataHandler
nodedatareq.open(mnodedataod,serverURL,true)
nodedatareq.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded")
nodedatareq.send(nodedataParam)
}
}

function nodedataHandler() {

var ready= nodedatareq.readyState
var respStr=null
var index
if( ready == READY_STATE_COMPLETE ) {
respStr=nodedatareq.responseText

// plot data

timer = setTimeout(sendNodeRequest, 1000)
}
}
function resizeEventHandler(target, e)
{
// do nothing
}

buidGraphs()
timer = setTimeout(sendNodeRequest, 1000)
</script>

This works fine advancing the graphs and looping until I resize the
window. Once I do that the first time it reenters sendNodeRequest() I
get an error that initXMLHTTPRequest is undefined. I tried inlining the
code for initXMLHTTPRequest into sendNodeRequest but then it complains
thay nodedatareq is undefined. It is as if it has completely lost all of
its history and has no context or scope.

I have tried using

window.onresize = function(e) {resizeEventHandler(e);};

instead of window.addEventListener. I tried

window.onresize = function(e) {resizing = 1;};

so it didn't have to call a function, but that had the same results.

I also tried putting the call to window.addEventListener before the call
to buildGraphs. Then it didn't die but the event was not seviced either.

Anyone have any suggestions?

Thanks

Howard
Dec 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Howard wrote:

If you don't return false in your event handlers, the browsers will
quite cheerfully continue in their default behavior, the kind which is
driving you crazy. Once you start returning false they will do what you
ask and no more.
window.onresize = function(e) {resizeEventHandler(e);};

instead of window.addEventListener. I tried

window.onresize = function(e) {resizing = 1;};

so it didn't have to call a function, but that had the same results.
window.onresize = function(e) {resizeEventHandler(e); return false};
window.onresize = function(e) {resizing = 1; return false};

function resizeEventHandler(target, e)
{
// do nothing and fail, return false for teh win
return false;
}

Note that not all browsers pass e.

if (e == null) { e = window.event; }

will make the code more cross browser compliant.

No idea why a resize event would trip your application up, not enough
information was supplied in the pseudo code. So there's no guarantee
this will work.

G'luck.

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Dec 16 '06 #2

P: n/a
Howard wrote:
I am having a problem that is driving me crazy and I am
whits end. I am hoping that someone here can help me. I
have a client server application where the client is
drawing a progressive bar graph based on data retrieved
from the server once a second. The basic loop looks
like this;

</HEAD>
<script language="JavaScript1.2">
^^^
This is silly in a script that requires the use of an XML HTTP request
object. You certainly do not want browsers that support the aberrant
type-conversion and alternative syntax of JavaScript 1.2 to actually
employ that language version.
>
var nodedatareq;
function buildGraphs() {

document.writeln("<BODY>");

// build graphs and add them

document.writeln("</BODY>");

window.addEventListener("resize",resizeEventHandle r, false);
}
<snip>
This works fine advancing the graphs and looping until I
resize the window. Once I do that the first time it reenters
sendNodeRequest() I get an error that initXMLHTTPRequest is
undefined. I tried inlining the code for initXMLHTTPRequest
into sendNodeRequest but then it complains thay nodedatareq
is undefined. It is as if it has completely lost all of its
history and has no context or scope.
<snip>

That is precisely what has happened. If you use -document.write - or
document.writeln - after the original document has loaded you are
re-opening the document, removing all of its existing contents, and
associated script functions and variables, and replacing the document
from scratch. You may not be calling either of the - write - methods
after the first loading of the document (though you also may as the
request is asynchronous and the time for the response to start will be
influenced by many factors, including network traffic), but as soon as
you re-size handler re-calls the method that does the wiring the current
document, and associated scripts, are removed and replaced.

Richard.
Dec 16 '06 #3

P: n/a
Lee
Howard said:
>
I am having a problem that is driving me crazy and I am whits end.
You're at the end of the smallest part imaginable because
Javscript seems to be allowing it's mind to run loose?
--

Dec 16 '06 #4

P: n/a
Thank you for your response

Richard Cornford wrote:
Howard wrote:
<snip>
></HEAD>
<script language="JavaScript1.2">
^^^
This is silly in a script that requires the use of an XML HTTP request
object. You certainly do not want browsers that support the aberrant
type-conversion and alternative syntax of JavaScript 1.2 to actually
employ that language version.
So, I am better off just using <scriptor some other version

<snip>
but as soon as
you re-size handler re-calls the method that does the wiring the current
document, and associated scripts, are removed and replaced.
I am not sure what you are saying here (a function of my lack of
knowledge I am sure). Is the location of my setting the callback the
problem or is the act of having a callback on the window object the
problem? I have tried moving the setting of the callback to other
locations in the script outside the <body>...</bodysection. If I move
the setting to before it, the callback never gets called. If I move it
after I get the same undefined. If setting the callback on the window
object is the problem, where is a better place to put it?

Thanks again

Howard

>
Richard.

Dec 18 '06 #5

P: n/a
Lee
Howard said:
>
Thank you for your response

Richard Cornford wrote:
>Howard wrote:

<snip>
>></HEAD>
<script language="JavaScript1.2">
^^^
This is silly in a script that requires the use of an XML HTTP request
object. You certainly do not want browsers that support the aberrant
type-conversion and alternative syntax of JavaScript 1.2 to actually
employ that language version.

So, I am better off just using <scriptor some other version

<snip>
>but as soon as
you re-size handler re-calls the method that does the wiring the current
document, and associated scripts, are removed and replaced.

I am not sure what you are saying here (a function of my lack of
knowledge I am sure). Is the location of my setting the callback the
problem or is the act of having a callback on the window object the
problem?
Your script tag should be:
<script type="text/javascript">

The problem is that you use document.write after the page has loaded.
The first thing document.write() does is to clear the page memory.
You need to choose another method of updating the page, such as
changing the innerHTML property of some component.
--

Dec 18 '06 #6

P: n/a
Got it, and fixed it. Thanks

Howard
Lee wrote:
Howard said:
>Thank you for your response

Richard Cornford wrote:
>>Howard wrote:
<snip>
>>></HEAD>
<script language="JavaScript1.2">
^^^
This is silly in a script that requires the use of an XML HTTP request
object. You certainly do not want browsers that support the aberrant
type-conversion and alternative syntax of JavaScript 1.2 to actually
employ that language version.
So, I am better off just using <scriptor some other version

<snip>
>>but as soon as
you re-size handler re-calls the method that does the wiring the current
document, and associated scripts, are removed and replaced.
I am not sure what you are saying here (a function of my lack of
knowledge I am sure). Is the location of my setting the callback the
problem or is the act of having a callback on the window object the
problem?

Your script tag should be:
<script type="text/javascript">

The problem is that you use document.write after the page has loaded.
The first thing document.write() does is to clear the page memory.
You need to choose another method of updating the page, such as
changing the innerHTML property of some component.

Dec 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.