Javascript onLoad error "Object doesn't support this action"

K

KathyB

I have the following script in an html page:

function goToPosition()
{
varGoTo = document.write(document.cookie("Position"));
document.scrollTo(0, varGoTo);
}
</head>
<body onload="goToPosition()">

When I put the onload="goToPosition()" in, I get the error message
"object doesn't support this action". I've also tried putting in after
the </body> tag as another script...no go.

I'm setting the cookie with these lines in another function:
varScroll = document.body.scrollTop; document.cookie("Position") =
varScroll;

Any clues/suggestions greatly appreciated. I recently posted a similar
question, but rearranged how I was doing it, still no luck, so I'm
trying again.

Thanks.
Kathy
 
K

Klaus Johannes Rusch

KathyB said:
I have the following script in an html page:

function goToPosition()
{
varGoTo = document.write(document.cookie("Position"));
document.scrollTo(0, varGoTo);
}
</head>
<body onload="goToPosition()">

When I put the onload="goToPosition()" in, I get the error message
"object doesn't support this action". I've also tried putting in after
the </body> tag as another script...no go.

Any clues/suggestions greatly appreciated. I recently posted a similar
question, but rearranged how I was doing it, still no luck, so I'm
trying again.

A document.write after the document has fully loaded will _replace_ the current document with the new
content, you can either place an empty div in the body and update the innerHTML or innerText propertly of
that div, or use window.alert to show the information, but you cannot use document.write.
 
R

Richard Cornford

KathyB said:
I have the following script in an html page:

function goToPosition()
{
varGoTo = document.write(document.cookie("Position"));
document.scrollTo(0, varGoTo);

scrollTo is a global function (property of the window object) so:-

window.scrollTo(0, varGoTo);
- or -
scrollTo(0, varGoTo);

-would work better. But - document.write( ... ) - in/after the onload
event (when the document has been closed) will clear the current
document and replace it (along with JavaScript functions and variables.
}
</head>
<body onload="goToPosition()">

When I put the onload="goToPosition()" in, I get the
error message "object doesn't support this action". I've
also tried putting in after the </body> tag as another
script...no go.

After the body tag is an invalid location for a script tag, just before
the body tag would be OK (and a much better place to be doing -
document.write( ... ) - as the document will not yet have been closed.
I'm setting the cookie with these lines in another
function:
varScroll = document.body.scrollTop;

The scroll offset is not always available as body.scrollTop. On many
browsers that value is available as window.pageYOffset and on IE 5.5+
browsers the scrollTop value should be read from
document.documentElement instead of document.body when the page is in
"standards" mode (document.compatMode == 'CSS1Compat'):-

var varScroll = 0;
if(typeof pageYOffset == 'number'){
varScroll = pageYOffset;
}else if((document.compatMode)&&
(document.compatMode == 'CSS1Compat')&&
(document.documentElement)){
varScroll = document.documentElement.scrollTop;
}else if(document.body){
varScroll = document.body.scrollTop;
}
document.cookie("Position") = varScroll;
<snip>

document.cookie - is a string property of the document object and not a
function so this syntax is incorrect.

Richard.
 
K

Kathy Burke

Thanks for the responses. I put the original together from other
posts/examples, but now realize that I should not have used
document.write...I just need to get the cookie value to set as the
varScroll value, not actually write it?

I still can't seem to get the cookie value (never used it before!).

Re: document.cookie - is a string property of the document object and
not a function so this syntax is incorrect...I've actually seen this
used in example(s) so now I'm confused yet again.

Thanks for the help!

Kathy
 
R

Richard Cornford

Re: document.cookie - is a string property of the document
object and not a function so this syntax is incorrect...I've
actually seen this used in example(s) so now I'm confused yet
again.

Server-side ASP JScript, I think, has a cookie function on either the
request or response objects (or both) that you may have seen used in
this way. For client side cookie work you will have to find your own set
of cookie functions. You might have a look at:-

<URL: http://jibbering.com/faq/#FAQ4_4 > and follow the link in that
section as it leads to a page with example cookie functions and some
explanation of them and their use.

Richard.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top