HELP: Changing ilayer src from a href link...

B

Brandon Hoppe

I'm trying to change the src of an ilayer in the parent document from a link inside the
ilayer. I'm not able to get it to work. All that happens is Netscape 4 crashes. This is
for Netscape 4 only.


For example, here is the main page:

<html>
<head>
<script type="text/javascript">
function updateILayer(obj) {
parent.document.layers["layer1"].src = obj.href;
}
</script>
</head>

<body>
<ilayer name="layer1" id="layer1" src="home.htm">
</ilayer>
</body>
</html>

And here's the home.htm src:

<html>
<head>
</head>

<body>
Click this link to go to test page:
<a onClick="parent.updateILayer(this);" href="test.htm">test</a>
</body>
</html>

And here's the test.htm src:

<html>
<head>
</head>

<body>
Click this link to go to home page:
<a onClick="parent.updateILayer(this);" href="home.htm">home</a>
</body>
</html>
 
B

Brandon Hoppe

Well, to keep it from crashing when you click on the link, I've change the function as such:

function updateILayer(obj) {
eval("parent.document.layer1.src=" + obj.href);
}

But it still doesn't work. It just goes to that link and doesn't change the ilayer src. I
can tell because I've add <hr> around the <ilayer> so if the ilayer src is changed then
the <hr> would still be there.
 
L

Lasse Reichstein Nielsen

Brandon Hoppe said:
Well, to keep it from crashing when you click on the link, I've change the function as such:

function updateILayer(obj) {
eval("parent.document.layer1.src=" + obj.href);
}

Don't use "eval", ever. There are better ways to do what you want
(where better is several of: shorter, faster, uses less momory, and is
less error prone). The exceptions are so few and rare, that you are
unlikely to hit one by accident.

If obj.href contains "http://www.example.com", your construction above
will try to eval the following string:
"parent.document.layer1.src=http://www.example.com"
Notice that there are no quotes around the part after the "=", so
it is just one big syntax error.

Try:

parent.document.getElementById("layer1").src=obj.href

(layers are generally not represented by global variables, you need
to find them the hard way).

You could probably also do:
parent.layers["layer1"].location.href = obj.href;
But it still doesn't work. It just goes to that link and doesn't
change the ilayer src.

Guess: you need to return false.
I can tell because I've add <hr> around the
<ilayer> so if the ilayer src is changed then the <hr> would still be
there.

Yep, remember to return false:
<a href="test.htm"
onclick="parent.updateILayer(this);return false">test</a>
Returning false prevents the normal operation of the link (following
it).

/L
 
B

Brandon Hoppe

Lasse said:
Don't use "eval", ever. There are better ways to do what you want
(where better is several of: shorter, faster, uses less momory, and is
less error prone). The exceptions are so few and rare, that you are
unlikely to hit one by accident.

If obj.href contains "http://www.example.com", your construction above
will try to eval the following string:
"parent.document.layer1.src=http://www.example.com"
Notice that there are no quotes around the part after the "=", so
it is just one big syntax error.

Yeah, I've never used eval but I was just trying anything at this point.
Try:

parent.document.getElementById("layer1").src=obj.href

(layers are generally not represented by global variables, you need
to find them the hard way).
Nope.

You could probably also do:
parent.layers["layer1"].location.href = obj.href;
Nope.

Yep, remember to return false:
<a href="test.htm"
onclick="parent.updateILayer(this);return false">test</a>
Returning false prevents the normal operation of the link (following
it).

Oh yeah, forgot to return false. Ok, I've added return false but it still just goes to
that link. I've tried these lines in the function call but neither worked:

parent.document.layer1.src=obj.href;
parent.document.getElementById("layer1").src=obj.href;
parent.document.layers["layer1"].src=obj.href;
parent.layers["layer1"].location.href=obj.href;

I've read you can change the ilayer src link but I haven't seen it done yet lol :)
 
B

Brandon Hoppe

Hell, I've even simplified so that the link is on the main page and I'm trying to change
the ilayer src:

<html>
<head>
<!--
parent.document.layer1.src=obj.href;
parent.document.getElementById("layer1").src=obj.href;
parent.document.layers("layer1").src=obj.href;
parent.document.layers["layer1"].src=obj.href;
parent.layers["layer1"].location.href=obj.href;
-->
<script type="text/javascript">
function updateILayer(obj) {
document.layer1.src=obj.href;
}
</script>
</head>

<body>
<a onClick="updateILayer(this);return false;" href="test.htm">TEST</a>
<a onClick="updateILayer(this);return false;" href="home.htm">HOME</a>
<hr>
<ilayer name="layer1" id="layer1" src="home.htm">
</ilayer>
<hr>
</body>
</html>

This doesn't work either. The ilayer area between the <hr> just goes blank when I click
the link.


Brandon said:
Lasse said:
Don't use "eval", ever. There are better ways to do what you want
(where better is several of: shorter, faster, uses less momory, and is
less error prone). The exceptions are so few and rare, that you are
unlikely to hit one by accident.

If obj.href contains "http://www.example.com", your construction above
will try to eval the following string:
"parent.document.layer1.src=http://www.example.com"
Notice that there are no quotes around the part after the "=", so
it is just one big syntax error.


Yeah, I've never used eval but I was just trying anything at this point.
Try:

parent.document.getElementById("layer1").src=obj.href

(layers are generally not represented by global variables, you need
to find them the hard way).

Nope.

You could probably also do:
parent.layers["layer1"].location.href = obj.href;

Nope.

Yep, remember to return false:
<a href="test.htm" onclick="parent.updateILayer(this);return
false">test</a>
Returning false prevents the normal operation of the link (following
it).


Oh yeah, forgot to return false. Ok, I've added return false but it
still just goes to that link. I've tried these lines in the function
call but neither worked:

parent.document.layer1.src=obj.href;
parent.document.getElementById("layer1").src=obj.href;
parent.document.layers["layer1"].src=obj.href;
parent.layers["layer1"].location.href=obj.href;

I've read you can change the ilayer src link but I haven't seen it done
yet lol :)
 
R

Richard Cornford

Brandon Hoppe said:
Hell, I've even simplified so that the link is on the main page
and I'm trying to change the ilayer src:
<snip>

It is a very long time since I have used an ILAYER, and I am not at home
so I cannot check the book that I have that lists its characteristics,
but I seem to remember that you just cannot change their - src -
property.

As I recall the approach that works is to nest a LAYER inside the ILAYER
and change its - src - instead. That required that the property accessor
for the - src - access the outer ILAYER in the document and then the
LAYER within the document.layers collection of the ILAYER.

Developing exclusively for Netscape 4 is an odd thing to be doing in
2004.

Richard.
 
B

Brandon Hoppe

Richard said:
As I recall the approach that works is to nest a LAYER inside the ILAYER
and change its - src - instead. That required that the property accessor
for the - src - access the outer ILAYER in the document and then the
LAYER within the document.layers collection of the ILAYER.

Now that works great if I change the layer src from a link in the parent document.

But if I use a link inside the src document of the layer, then Netscape crashes. Any ideas
why?
 
R

Richard Cornford

Brandon Hoppe said:
Now that works great if I change the layer src from a link in the
parent document.

But if I use a link inside the src document of the layer, then
Netscape crashes. Any ideas why?

For future reference, If you ask questions on this group and don't
provide access to the code under discussion (and preferably in an easily
testable form) you are likely to find the process of getting help at
least slow and often fruitless.

Without code I can only guess at what you are doing so if my best guess
does not correspond with what you are doing (that is, you are doing
something other than what I guess you are doing and that is causing the
problem) then you will have wasted my time answering at all. Which
wouldn't encourage me to help next time (or anyone else who isn't
willing to show their code).

However, there certainly are problems with Netscape 4 while trying to
swap content from within the content that is to be replaced (as I recall
most apparent on Unix/Linux versions). I don't recall if there was a
good solution (as I said, it has been a long time) but I would expect
that decoupling the swapping of the content using a (global) setTimeout
call might work.

You would create a global function that actually swapped the - src -
that was called with the target URL as a string parameter (and possibly
any variable information required to resolve the reference to the ILAYR
and LAYER elements). And then, from within the layer, call
window.setTimeout with an argument that would call that function and
provide the required URL, etc, as string literal parameters, with a
smallish delay. That way the execution of the - src - swapping function
should behave the same as swapping the - src - from outside of the
LAYER.

Richard.
 
B

Brandon Hoppe

Richard said:
For future reference, If you ask questions on this group and don't
provide access to the code under discussion (and preferably in an easily
testable form) you are likely to find the process of getting help at
least slow and often fruitless.

Without code I can only guess at what you are doing so if my best guess
does not correspond with what you are doing (that is, you are doing
something other than what I guess you are doing and that is causing the
problem) then you will have wasted my time answering at all. Which
wouldn't encourage me to help next time (or anyone else who isn't
willing to show their code).

Of course. Sorry about that. I usually always do that. I completely agree with you. The
code that I have now is pasted at the bottom of this message.
However, there certainly are problems with Netscape 4 while trying to
swap content from within the content that is to be replaced (as I recall
most apparent on Unix/Linux versions).

Ahh. You've hit up on a good point there. I've been trying this with NN 4.7 on Solaris.
When I tried it using NN 4.7 on Windows, it worked perfectly. But I'd still like to get
this working on UNIX systems as well.
I don't recall if there was a
good solution (as I said, it has been a long time) but I would expect
that decoupling the swapping of the content using a (global) setTimeout
call might work.

You would create a global function that actually swapped the - src -
that was called with the target URL as a string parameter (and possibly
any variable information required to resolve the reference to the ILAYR
and LAYER elements). And then, from within the layer, call
window.setTimeout with an argument that would call that function and
provide the required URL, etc, as string literal parameters, with a
smallish delay. That way the execution of the - src - swapping function
should behave the same as swapping the - src - from outside of the
LAYER.

Richard.

Ok, I've tried it using window.setTimeout(). But in the layer is this error when I click
any link, either on the main page or in home/test page:

Not Found

The requested object does not exist on this server. The link you
followed is either outdated, inaccurate, or the server has been
instructed not to let you have it. Please inform the site
administrator of the referring page.


Here's the code:

<!-- MAIN.HTM -->
<html>
<head>
<script type="text/javascript">
function changeLayer(obj) {
document.ilayerName.document.layerName.src = obj.href;
}
</script>
</head>

<body>
COOL<br>
<a href="./test.htm" onclick="window.setTimeout('changeLayer(this)', 100); return
false;">TEST</a><br>
<a href="./home.htm" onclick="window.setTimeout('changeLayer(this)', 100); return
false;">HOME</a><br>
<hr>
<ilayer name="ilayerName" id="ilayerName" width=500 height=500>
<layer name="layerName" id="layerName" src="home.htm" width=500 height=500></layer>
</ilayer>
<hr>
</body>
</html>

<!-- HOME.HTM -->
<html>
<head>
</head>
<body>
<a href="test.htm" onclick="window.setTimeout('parent.changeLayer(this)', 100); return
false;">TEST</a><br>
HOME PAGE
</body>
</html>

<!-- TEST.HTM -->
<html>
<head></head>
<body>
<a href="home.htm" onclick="window.setTimeout('parent.changeLayer(this)', 100); return
false;">HOME</a><br>
TEST PAGE
</body>
 
R

Richard Cornford

Brandon Hoppe said:
Richard Cornford wrote:
function changeLayer(obj) {
document.ilayerName.document.layerName.src = obj.href;
}

I said that you should use a string parameter because you cannot
reliably use an object reference in the string argument for a steTimeout
call unless that reference is via, or to, a global property. String
argument setTimout code is always executed in a global context
(specifically, the global context to which the setTimout function that
is called belongs).

function changeLayer(url) { //pass the url as a string
document.ilayerName.document.layerName.src = url;
}

<a href="./test.htm" onclick="window.setTimeout('changeLayer(this)',
100); return false;">TEST</a><br>

you should not need the setTimout call from code outside the layer. You
may as well call the - changeLayer - function directly. Changing the
object reference argument to a string:-

onclick="changeLayer(this.href);return false;"

<!-- HOME.HTM -->
<a href="test.htm"
onclick="window.setTimeout('parent.changeLayer(this)', 100);
return false;">TEST</a><br>
<snip>

When the string argument to this setTimout call is executed in the
global context the - this - keyword will refer to the window (global)
object. It does not have a - href - property, so it is the value -
undefined - that will be assigned to the - src - property of the LAYER.
Assuming that the internal setter for - src - type-converts its
parameter into a string, the URL assigned will be "undefined" and that
resource will not be found. Also, there is no point in prefixing the
call to - changeLayer - with "parent." as the setTimout code will
execute in a context where - changeLayer - is directly accessible. Try:-

onclick="window.setTimeout('changeLayer(\''+this.href+'\')', 100);
return false;"

- (correcting for line wrapping) instead.

Richard.
 
B

Brandon Hoppe

Richard said:
When the string argument to this setTimout call is executed in the
global context the - this - keyword will refer to the window (global)
object. It does not have a - href - property, so it is the value -
undefined - that will be assigned to the - src - property of the LAYER.
Assuming that the internal setter for - src - type-converts its
parameter into a string, the URL assigned will be "undefined" and that
resource will not be found. Also, there is no point in prefixing the
call to - changeLayer - with "parent." as the setTimout code will
execute in a context where - changeLayer - is directly accessible. Try:-

onclick="window.setTimeout('changeLayer(\''+this.href+'\')', 100);
return false;"

- (correcting for line wrapping) instead.

Richard.

Well, it works....sorta lol

It changes the the src of the layer, but now it just delays Netscape from crashing lol

Actually, Netscape crashes when I move the mouse. After I click the link, if I don't move
the mouse at all, Netscape will stay up until I move the mouse. Then it crashes. lol this
is so funny

Here's the changed code:

<html>
<head>
<script type="text/javascript">
function changeLayer(url) {
document.ilayerName.document.layerName.src = url;
}
</script>
</head>
<body>
<a href="test.htm" onclick="changeLayer(this.href)', 100); return false;">TEST</a><br>
<a href="home.htm" onclick="changeLayer(this.href)', 100); return false;">HOME</a><br>
<hr>
<ilayer name="ilayerName" id="ilayerName" width=500 height=500>
<layer name="layerName" id="layerName" src="home.htm" width=500 height=500></layer>
</ilayer>
<hr>
</body>
</html>

<html>
<head>
</head>
<body>
<a href="test.htm" onclick="window.setTimeout('changeLayer(\''+this.href+'\')', 100);
return false;">TEST</a><br>
HOME PAGE
</body>
</html>

<html>
<head></head>
<body>
<a href="home.htm" onclick="window.setTimeout('changeLayer(\''+this.href+'\')', 100);
return false;">HOME</a><br>
TEST PAGE
</body>
 
G

Grant Wagner

Brandon said:
Well, it works....sorta lol

It changes the the src of the layer, but now it just delays Netscape from crashing lol

Actually, Netscape crashes when I move the mouse. After I click the link, if I don't move
the mouse at all, Netscape will stay up until I move the mouse. Then it crashes. lol this
is so funny

Here's the changed code:

<html>
<head>
<script type="text/javascript">
function changeLayer(url) {
document.ilayerName.document.layerName.src = url;
}
</script>
</head>
<body>
<a href="test.htm" onclick="changeLayer(this.href)', 100); return false;">TEST</a><br>
<a href="home.htm" onclick="changeLayer(this.href)', 100); return false;">HOME</a><br>
<hr>
<ilayer name="ilayerName" id="ilayerName" width=500 height=500>
<layer name="layerName" id="layerName" src="home.htm" width=500 height=500></layer>
</ilayer>
<hr>
</body>
</html>

<html>
<head>
</head>
<body>
<a href="test.htm" onclick="window.setTimeout('changeLayer(\''+this.href+'\')', 100);
return false;">TEST</a><br>
HOME PAGE
</body>
</html>

<html>
<head></head>
<body>
<a href="home.htm" onclick="window.setTimeout('changeLayer(\''+this.href+'\')', 100);
return false;">HOME</a><br>
TEST PAGE
</body>

The following works fine with Netscape 4.78 here:

<script type="text/javascript">
function updateILayer(obj) { document.layer1.src = obj.href; }
</script>
<a onClick="updateILayer(this);return false;" href="http://www.yahoo.com">TEST</a>
<a onClick="updateILayer(this);return false;" href="http://www.microsoft.com">HOME</a>
<hr>
<ilayer name="layer1" id="layer1" src="http://www.microsoft.com">
</ilayer>
<hr>

If it's crashing for you, then I'd suggest you remove Netscape, copy your /User directories,
delete /Program Files/Netscape and reinstall Netscape 4.x

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
B

Brandon Hoppe

Grant said:
The following works fine with Netscape 4.78 here:

<script type="text/javascript">
function updateILayer(obj) { document.layer1.src = obj.href; }
</script>
<a onClick="updateILayer(this);return false;" href="http://www.yahoo.com">TEST</a>
<a onClick="updateILayer(this);return false;" href="http://www.microsoft.com">HOME</a>
<hr>
<ilayer name="layer1" id="layer1" src="http://www.microsoft.com">
</ilayer>
<hr>

If it's crashing for you, then I'd suggest you remove Netscape, copy your /User directories,
delete /Program Files/Netscape and reinstall Netscape 4.x

Grant, please read the previous messages. What you have here isn't the issue. Of course
this works. I'm trying to update the src from INSIDE the ilayer.
 
R

Richard Cornford

Brandon Hoppe wrote: said:
Grant, please read the previous messages. What you have here isn't
the issue. Of course this works. I'm trying to update the src from
INSIDE the ilayer.

Except that what Grant is pointing out is that, for him at least,
swapping the - src - on an ILAYER is possible in addition to a LAYER. I
finally got round to looking up the Netscape 4 layer tags in that book
and I was wrong about - src - swapping only working with LAYER. What I
was wrongly remembering was the fact that you can use document.write on
a LAYER but not on an ILAYER. On the other hand I can't get grants code
to work on Netscape 4.78 on Windows or 4.77 on Linux, and the only
difference is that I was testing with local (file:// protocol) files
instead of HTTP protocol resources (that can sometimes make a big
difference).

In any event, I managed to reproduce the crashes exactly as described on
Linux Netscape 4.77. I had also noticed while looking in my book that
Netscape 4 layers have a - load - method (two arguments, a URL and a
pixel width value) that I figured might be better than setting the -
src -, but in the context of the original code it made no difference.

However, I did an experiment with having two LAYERs as children of the
ILAYER, one hidden and z-indexed under the other. And changing the
source in the hidden layer and then swapping their z-indexes and
visibility. I made two test versions, one assigning to the - src -
property and the other calling the - load - method. They both worked
better than the original, didn't crash as soon as the mouse was moved,
but repeatedly swapping the document from within itself (quite quickly)
did crash the browser. The version that used the - load - method seemed
slightly more robust in this respect:-

<html>
<head>
<script type="text/javascript">
var currentLayer = 0;
function changeLayer(url) {
var oldLayer = document.ilayerName.document['lName'+currentLayer];
currentLayer = (currentLayer+1)%2;
var newLayer = document.ilayerName.document['lName'+currentLayer];
newLayer.load(url, 500);
oldLayer.visibility = 'hide';
oldLayer.zIndex = 1;
newLayer.zIndex = 2;
newLayer.visibility = 'show';
}
</script>
</head>
<body>
<a href="test.html"
onclick="changeLayer(this.href); return false;">TEST</a><br>
<a href="home.html"
onclick="changeLayer(this.href); return false;">HOME</a><br>
<hr>
<ilayer name="ilayerName" id="ilayerName" width=500 height=500>
<layer name="lName0" id="lName0" src="home_load.html"
width=500 height=500 z-index="2"></layer>
<layer name="lName1" id="lName1" src="home_load.html"
width=500 height=500 z-index="1" visibility="hide"></layer>
</ilayer>
<hr>
</body>
</html>

You will have to try it out and see what you think.

Richard.
 
B

Brandon Hoppe

Way to go Richard :)

This works. Netscape hasn't crashed on a Solaris machine!

Thanks for all the hard work.

Brandon
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top