473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DHTML Problem

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
3 1904
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul THompson | last post by:
I have a piece of code something like the following: START OF CODE <html><head><title>The Wizard</title></head> <body> <h1>Welcome to Joe's Vet Clinic</h1> <div id="part1"...
3
by: Henning Heil | last post by:
hello all! I have a little problem with a DHTML feature which works fine with most browsers but not with NS4. an example can be found here:...
2
by: Frances Del Rio | last post by:
please, can you go to http://www.francesdelrio.com/dhtml/ when you click the name of the character the font color for all the lines of that character should change.. this is a simple DHTML...
2
by: mr_burns | last post by:
hi there, i would like a book that will explain concepts of javascript and dhtml instead of, for example, ten tutorials on how to do specific things in js or dhtml. ideally a book thats can...
1
by: Steve | last post by:
Hi; I have heard about a few very good books on DHTML, but the most modern ones seem to have been published in 2002. Have any _significant_ changes in DHTML or the standard DOM happened since...
5
by: Sura | last post by:
Hi I have designed a simple DHTML menu which comes over a flash animation when it appears. I have set the window mode as transparent in the flash project. This menu is showing fine when seen...
4
by: bbass | last post by:
thanks to all that replyied to my previous post with the following code in question: <a href="merc.htm" target="_new_merc" onfocusout=window.close class="left_link"> i understand that the...
17
by: Jacques Jamain | last post by:
hello, in a dhtml page generated from php I want to include a file which is in fact the body of the dhtml page. My hosting ISP does not allow SSI but supports php includes. In the dhtml page, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.