Coding problem


R

Robert Baer

The following code "works" in a strange way; see the comments for
details.
1) why is that document.write necessary for operation?
2) why doesn't the altered variable "title" remain changed?
3) i would like to change displayed page, based on map visits

Help? Comments?


<SCRIPT type="text/javascript">
// <img id="page" src=title alt="Codatron page 1">
// src="images/CodatronP1.png"
var title="images/CodatronP1.png";
var dir=-1;
var num=3;
function incrPage(dir,title)
{
num=title.substr(16,1).valueOf();
if (dir===-1)
{
num=num-1;
if (num===0) {num=7;}
}
else
{
num=num+1;
if (num===8) {num=1;}
}
title=title.substr(0,16)+num+title.substr(17);
document.write("###"+title+"*");
// above correct at page 7 being "previous" to page 1 (sequence 1,2,,7,1
etc)
// *NOTE* the document.write MUST exist for the map visits to work ???
}
incrPage(dir,title);
// document.write("***"+title+"* *#");
// above is wrong, with title equal to unchanged initial def

// document.getElementById("page").innerHTML=title;
</script>

<div>
<img src="ShuttleN.png" alt="Select" usemap="#PageSelect"
</div>
<div>
<!-- visiting mapped areas give "###-1*" or "###1*" in new screen -->
<map id="images/CodatronP5.png" name="PageSelect">
<area shape="rect" alt="Minus" coords="65,0,135,35"
src="N.PNG" onMouseover="incrPage(-1,id)" >
<area shape="rect" alt="Plus" coords="590,0,668,35"
src="P.PNG" onMouseover="incrPage(1,id)" >
<area shape="default" nohref="nohref" alt="" >
</map>
</div>

<!-- display page one; would like to change that based on map visits -->
<div class=" container_24">
<div class="wrapper">
<img id="page" src="images/CodatronP1.png" alt="Page">
</div>
</div>
 
Ad

Advertisements

D

Doug Miller

Robert Baer said:
The following code "works" in a strange way; see the comments for
details.
1) why is that document.write necessary for operation?
2) why doesn't the altered variable "title" remain changed?
3) i would like to change displayed page, based on map visits

Help? Comments?

You'll probably have better luck posting in one of the javascript groups.
 
B

Ben Bacarisse

Robert Baer said:
I have found only one JS group, and it is essentially dead.
Hopefully, http://www.codingforums.com can help.
Thanks.

comp.lang.javascript is a possibility.

And your questions:

1) It's not, but it's one reasonable way to inject some text into an
existing document.

2) This is a basic property functions and arguments. Argument passing
is by copying:

vat title = "abc";

function test1(t) { t = "def"; }
function test2(title) { title = "def"; }

test1(title);
test2(title);
// Both functions are essentially the same and neither call changes
// the value of the "outer" variable called title.

If you need a changed value, get the function to return it.

title = calculateNewTitle(title);

3) Pass
 
R

Robert Baer

Ben said:
comp.lang.javascript is a possibility.
* Not functional, been empty for ages.
And your questions:

1) It's not, but it's one reasonable way to inject some text into an
existing document.
* Function does not work without that line of code. I agree, it should.
The "return" statement oes nothing except guarantee that JS proceeds
no further (and that agrees with the documentation as far as execution
is concerned).
2) This is a basic property functions and arguments. Argument passing
is by copying:

vat title = "abc";

function test1(t) { t = "def"; }
function test2(title) { title = "def"; }

test1(title);
test2(title);
// Both functions are essentially the same and neither call changes
// the value of the "outer" variable called title.

If you need a changed value, get the function to return it.

title = calculateNewTitle(title);
* Oh! Thanks; the online "tutorial" does not give refinements like that.
2 out of 3 is excellent!
Thanks.
 
Ad

Advertisements

B

Ben Bacarisse

Robert Baer said:
* Not functional, been empty for ages.

I posted a reply there yesterday. I see messages there every day, more
or less, -- more that I see here, for example.
* Function does not work without that line of code. I agree, it should.
The "return" statement oes nothing except guarantee that JS proceeds
no further (and that agrees with the documentation as far as execution
is concerned).

Misunderstanding here. When I said that the document.write was not
necessary I meant that there are other ways to do the kind of thing that
document.write does. The specific question (which is what I think you
were asking) of why the code works with it, and does not work without
it, seems unanswerable without more data. The context of the code,
and the definition of "works" and "not works" being the main things I'd
want to know. I agree it looks odd, but that's all I can say.

<snip>
 
R

Robert Baer

Hot-Text said:
You're welcome

I was doing a Internet Archive: Wayback Machine
Looking for old Editor Software back in 1998
and one of the websites had a like to freewebmasterhelp
and I saw the JavaScript and remember you
With that reference, and a few hours of fiddling, i am _close_ to
getting what i want.
Thanx for the help!

Have seven images and want to cycle thru them + for 7,1,2,3,,6,7 and
- for 1,7,6,5,,2,1.
Using HTML usemap and specific image names, + increments its given
name in that part of code, and - decrements its given name in that part
of code; both work nicely.
Used two JS functions; crudely first called via mouseOver(), one
presents a given name and calls second that properly cycles thru the
numbers/names.
But it "steps" only once.
I think that if there was such a beastie as a HTML variable, i could
solve this in short order.

Got the code and query posted in codingforums.com .
 
M

meagain

Robert said:
Wow! Nicely done, good presentation, nice examples.
Tanks a lot!!

It is good, but does not directly answer the Original question
"why is document.write necessary."

Examine some javascript code, like
document.write(
"<div>"+Date().toLocaleString().substr(0,10).fontsize(6).bold()+txt.fontsize(6).bold()+"<br><br><\/div>"
);

- http://96.9.49.162/AccessBolton_files/NowPlaying.htm

and you should see why it is "necessary."
 
Ad

Advertisements

J

Jonathan N. Little

meagain said:
Hot-Text wrote:

The above link is obsolete at best, recommending such things as HTML
comments in script elements to "hide script from old browsers". Really?
If memory serves me wasn't that a version of Netscape 2?
It is good, but does not directly answer the Original question
"why is document.write necessary."

Examine some javascript code, like
document.write(
"<div>"+Date().toLocaleString().substr(0,10).fontsize(6).bold()+txt.fontsize(6).bold()+"<br><br><\/div>"
);

- http://96.9.49.162/AccessBolton_files/NowPlaying.htm

and you should see why it is "necessary."

"necessary"? The main problem with document.write is that it must be
done BEFORE the document finishes loading whereas using DOM you can add,
remove, and modify elements after the document loads.
 
R

Robert Baer

meagain said:
It is good, but does not directly answer the Original question
"why is document.write necessary."

Examine some javascript code, like
document.write(
"<div>"+Date().toLocaleString().substr(0,10).fontsize(6).bold()+txt.fontsize(6).bold()+"<br><br><\/div>"
);

- http://96.9.49.162/AccessBolton_files/NowPlaying.htm

and you should see why it is "necessary."
Finally found an un-documented trick by using
document.getElementById() "backwards".
Do not need document.write anywhere.
Am cycling pictures forward or backwards according to onMouseover
visits at 2 designated places.
Code:
<script type="text/javascript">
function incrPage(dir,title)
{
var num=+title.substr(16,1).valueOf();
if (dir===-1)
{
num=num-1;
if (num===0) {num=7;}
}
else
{
num=num+1;
if (num===8) {num=1;}
}
title=title.substr(0,16)+num+title.substr(17);
return title;
// Sequence is 1,2,,7,1 for plus, 1,7,6,,1 for minus
}

function SwPage(updn) {
var Pnam=document.getElementById("imgpage").src;
// above gives full URL, so must parse
var NamLoc=Pnam.search("images");
var ISnam=Pnam.substr(NamLoc);
var NUpg="";
if (updn ==="up") {
NUpg=incrPage(1,ISnam);
document.getElementById("imgpage").src = NUpg;
}
else {
NUpg=incrPage(-1,ISnam);
document.getElementById("imgpage").src = NUpg;
}
}
</script>
 
Ad

Advertisements

T

Tim Streater

[snip]
Finally found an un-documented trick by using
document.getElementById() "backwards".
[snip]

document.getElementById("imgpage").src = NUpg;

if you're referring to this, it's not a trick and it's not
"undocumented", it's SOP.

It's the same as doing this:

ptr = document.getElementById("imgpage");
ptr.scr = NUpg;

just that with what you wrote you get a pointer to the element when you
need it rather than ahead of time. There is some optimisation advantage
to my code if you are going to use this pointer lots of times.
 

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

Top