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

DHTML Problem

P: n/a
Hi.

I've copied some code from a book which will enable me to make a layer
around a page, my aim is to do something a little more complex but this I
thought would get me started.

Anyway, I've run into a problem before I even start! The script works OK, it
runs the image in from the left hand side quite nicely, great. Problem I
have it always scrolls up to the top of the page, when the link I click
maybe 2 or 3 screens below (does that make sence?). I thought if I was to
add a name attribute to the link I could get the co-ordinates of this and
the screen will not reposition.

I tried this and I've come unstuck! I need to get the name of the link into
the slide function ..... can some bright spark out there help me out please,
I'm just no JavaScript expert!

TIA.

Chris
<SCRIPT language="JavaScript">
var layername, xgoal, ygoal, xhop, yhop, delay=5;

function checkDHTML() {
if ((parseInt(navigator.appVersion)>=4) &&
((navigator.appName !="Netscape" &&
navigator.appVersion.indexOf("X11") == 1) ||
(navigator.appName != "Microsoft Internet Explorer" &&
navigator.appVersion.indexOf("Macintosh") == -1)))
{ return 1}
else
{ document.location="nodhtml.htm"; return 0 }
}

function makeName(layerID) {
if (navigator.appName=="Netscape")
{ refname = eval("document." + layerID) }
else
{ refname = eval("document.all." + layerID + ".style") }
return refname
}

function slide() {
if ((parseInt(layername.left) != xgoal) ||
(parseInt(layername.top) != ygoal))
{
layername.left = parseInt(layername.left) + xhop;
layername.top = parseInt(layername.top) + yhop;
window.setTimeout("slide()", delay)
}
}

</SCRIPT>

HTML Part.

<DIV ID='picture0' STYLE='position: absolute; left: -500px; top: 10px;
width: 300; z-index: 1'>
<img src='pics/114974744.gif'>
</DIV>
<A name="link0" href="#" onClick="layername=makeName('picture0'); yhop=0;
ygoal=10; xhop=40; xgoal=100; slide()">
Click me</A>
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Chris Leonard" <c.*************@btinternet.com> writes:
I've copied some code from a book which will enable me to make a layer
around a page, my aim is to do something a little more complex but this I
thought would get me started.
Books are dangerous. A lot of them aren't worth the paper they are
printed on. I fear you might have one of those.
Problem I have it always scrolls up to the top of the page, when the
link I click maybe 2 or 3 screens below (does that make sence?).
It sounds like a classical problem :)

You have a link of the form:
<a href="#" onclick="some javascript">
When you click it, the javascript is executed, and then you jump to the
top of the document. If you look at the address line, you will notice that
a "#" has been added to the address. That is because the browser follows
the link to "#", which brings it back to the same page, only at the top.

To avoid that the browser follows the link, the onclick handler must
return false:
<a href="" onclick="some javascript;return false">
(the "#" isn't needed, the empty string is a legal URL).
Now some comments on the actual code. Muhahahahah! :)
<SCRIPT language="JavaScript">
<script type="text/javascript">

The type attribute is mandatory, and the language attribute is
deprecated, in HTML 4.
function checkDHTML() {
if ((parseInt(navigator.appVersion)>=4) &&
((navigator.appName !="Netscape" &&
navigator.appVersion.indexOf("X11") == 1) || ^ should be -1, right?
Otherwise it won't match MS IE on Windows, which is probably not
what you want.
(navigator.appName != "Microsoft Internet Explorer" &&
navigator.appVersion.indexOf("Macintosh") == -1)))
{ return 1}
else
{ document.location="nodhtml.htm"; return 0 }
window.location.href="nodhtml.htm";

document.location isn't universally supported (i.e., it might fail
for exactly those browsers that you don't think have DHTML either).
No need to return after having dropped the entire page for a new one.
}
No DHTML unless:

-The version is 4+. Bad idea. A new browser that came out today would
be version 1.0 (although it would probably have to claim to be version
5+ to avoid problems with scripts like this). Only ever use the
appVersion for anything if you know the browser type (e.g., appName).

-It is not a non-Netscape on non-Unix(X11 actually) or non-Microsoft
IE on a non-macintosh.

Or more clearly: Not Netscape on Macintosh or IE on X11 (if the "1"
should have been a "-1"). That is probably still not what you wanted,
since it includes Netscape on Windows and any non-Netscape and non-IE
browser.

Luckily this function isn't used. :)
function makeName(layerID) {
if (navigator.appName=="Netscape")
{ refname = eval("document." + layerID) }
refname = document[layerID];

You definitly never need to use the eval function just to read the
value of a property or variable.
You probably never need to use the eval function at all.

Not all versions Mozilla (if any) will have the "layer" as a property
of the document object. I bet this code was created for Netscape 4,
and doesn't know that Mozilla reports navigator.appName=="Netscape".

In Mozilla (and derivatives like Netscape 7) and other modern browsers,
you access the "layer" as
refname = document.getElementById(layerID);
else
{ refname = eval("document.all." + layerID + ".style") }
refname = document.all[layerID].style;
return refname
}

function slide() {
if ((parseInt(layername.left) != xgoal) ||
(parseInt(layername.top) != ygoal))


The "left" and "top" properties are only present in IE and Netscape 4.
Most modern browser have the properties "offsetLeft" and "offsetTop"
which can only be read, not written. You use CSS to change position.

Don't trust your books too much, they can be bad for you.
At least realize that what they teach is very specific to IE and
Netscape 4, and shouldn't be used in modern pages.

Don't be discouraged, it takes some time to learn which books to trust
and which not to.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
In article <d6**********@hotpop.com>, Lasse Reichstein Nielsen <lr*@hotpop.com>
writes:
function checkDHTML() {
if ((parseInt(navigator.appVersion)>=4) &&
((navigator.appName !="Netscape" &&
navigator.appVersion.indexOf("X11") == 1) ||

^ should be -1, right?
Otherwise it won't match MS IE on Windows, which is probably not
what you want.
(navigator.appName != "Microsoft Internet Explorer" &&
navigator.appVersion.indexOf("Macintosh") == -1)))
{ return 1}
else
{ document.location="nodhtml.htm"; return 0 }


window.location.href="nodhtml.htm";

document.location isn't universally supported (i.e., it might fail
for exactly those browsers that you don't think have DHTML either).
No need to return after having dropped the entire page for a new one.
}


No DHTML unless:

-The version is 4+. Bad idea. A new browser that came out today would
be version 1.0 (although it would probably have to claim to be version
5+ to avoid problems with scripts like this). Only ever use the
appVersion for anything if you know the browser type (e.g., appName).

-It is not a non-Netscape on non-Unix(X11 actually) or non-Microsoft
IE on a non-macintosh.

Or more clearly: Not Netscape on Macintosh or IE on X11 (if the "1"
should have been a "-1"). That is probably still not what you wanted,
since it includes Netscape on Windows and any non-Netscape and non-IE
browser.

Luckily this function isn't used. :)


It probably mentions an onload in the body tag (in the book) to execute the
function to make sure the browser can handle the page.

Why all the utterly useless browser detection at all?
http://jibbering.com/faq/#FAQ4_26

http://www.metalusions.com/backstage/articles/8/

if(document.all && !document.getElementById) {
document.getElementById = function(id) {
return document.all[id];
}
}

skips the need for testing for document.all at all and allows the use of
getElementById, untested by me in anything but IE4.0 on a PC.
Anybody that can test that in a browser that supports .all, but not
..getElementById other than IE4? I seem to recall Jim mentioning one that even
supports .all and .layers at the same time.

if (document.getElementById)
{
//lets use getElementById
}
else
{
if (document.layers)
{
//lets use document.layers
}
else
{
alert('Your browser does not support the methods I need to make this page
work')
}
}

Now, if the browser supports what you want to try to do, it will do it.
Otherwise, it will degrade. And all without knowing, or even caring, what
browser is in use.

--
Randy
All code posted is dependent upon the viewing browser
supporting the methods called, and Javascript being enabled.
Jul 20 '05 #3

P: n/a
Thanks very much.

Not really the answer I was looking for !!

Oh well, perhaps you're right, I should have started off myself and not used
the damn book, which to be fair is out dated but it's all I have and is a
good reference .......... well, was a good reference in 2000 !!

Chris
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.