Click to See Complete Forum and Search --> : Out Of Memory


brendandonhue
06-04-2003, 05:34 PM
I am writing a script to get info from all links on the page and display it. It works fine, but I get an Out Of Memory error, even though the script still does its job.
Heres the script:
<a href="http://google.com">Google</a>
<script language="javascript">
function getLinks(number)
{
var max = document.links.length;
var number = number;
var links = new Array()
if (number < max)
{document.write("<pre>" + number + " " + document.links[number] + " " +document.links[number].innerText + "<br>" + "</pre>")}
else{};
getLinks(number+1)
}
getLinks(0)
</script>
I just need to fix this and add the link's protocol to the list...then I think I will make a bookmarklet out of this and it will be pretty cool :D
Any help is appreciated...

Khalid Ali
06-04-2003, 05:59 PM
There are few things that you can do.
1. the out of memory is being caused because you are calling getLinks(number) function recursively..


function getLinks(number){
var max = document.links.length;
var number = number;
var links = new Array()
if (number < max){
document.write("<pre>" + number + " " + document.links[number] + " " +document.links[number].innerText + "<br>" + "</pre>")
}else{

};
getLinks(number+1)
}


move the getLinks(numbers out of function or correct your logic in the function so that it won't run for everrrrr

2. typically I'd refrain from using document.write in the middle of a logical flow of the page it may cause un warranted errors.use a div element and then u can use innerHTML or inner text

brendandonhue
06-04-2003, 07:13 PM
Thanks a lot, ive made some progress,the error is gone by using a WHILE loop, but im not exactly sure how to get the results into a div? Got anymore insight for me?
<a href="http://google.com">Google</a>
<a href="http://yahoo.com">Yahoo</a>
<a href="http://aol.com">AOL</a>
<script language="javascript">
function getLinks(number)
{
var max = document.links.length;
var number = number;
var links = new Array()
if (number < max)
{document.write("<pre>" + number + " " + document.links[number] + " " +document.links[number].innerText + "<br>" + "</pre>");getLinks(number+1)}
else{};
}
getLinks(0)
</script>

brendandonhue
06-04-2003, 07:30 PM
Maybe I can get the results into a new window? That would work well. I checked out your example of Parent/Child Window Communication and I can't seeem to get it working.

What I had was
window.open("","child")

and changed document.write to child.document.write
Know what I did wrong?

Khalid Ali
06-04-2003, 07:53 PM
do it this way ...

var child = window.open("","child")

and then you do

child.document.open();
child.document.write(dataStr);
child.document.close();

This will work.

brendandonhue
06-04-2003, 08:00 PM
Thx, but still not quite workin, I must be doing something wrong. It opens a window, but only shows the data of the last link. I think its because the variable is getting cleared every time I run it....
<a href="http://google.com">Google</a>
<a href="http://yahoo.com">Yahoo</a>
<a href="http://aol.com">AOL</a>
<script language="javascript">
function getLinks(number)
{
var child = window.open("","child");
var max = document.links.length;
var number = number;
var links = new Array()
while (number < max)
{linkdata = "<pre>" + number + " " + document.links[number] + " " +document.links[number].innerText + "<br>" + "</pre>";getLinks(number+1);child.document.open();
child.document.write(linkdata)}
}
getLinks(0);
</script>

Khalid Ali
06-04-2003, 08:13 PM
What are you trying to do here..can you explain please.

Are you trying to get all of thelinks on a page and create print them on a child window?

JHL
06-04-2003, 08:14 PM
document.write() will overwrite everything in the document.
so you should put everything in a variable first before using document.write().

brendandonhue
06-05-2003, 04:46 AM
Yes khalid, thats what im trying to do.

Thx JHL im working on it.

Scriptage
06-05-2003, 05:52 AM
document.write does not overwrite everything in the document unless the document has already been rendered.
The script does not need functions, and a lot of the variables used. Here is a more efficient version of the script (that also works, lol).


<a href="http://google.com">Google</a>
<a href="http://yahoo.com">Yahoo</a>
<a href="http://aol.com">AOL</a>
<script language="javascript">
var child=window.open("","");
for(var i=0; i<document.links.length; i++){
child.document.write("<pre>" + eval(i+1) + " " + document.links[i] + " " +document.links[i].innerText + "<br>" + "</pre>");
}
</script>

Regards
Carl

brendandonhue
06-05-2003, 06:02 AM
Thanks scriptage, worked perfectly! I just edited it slightly because I wanted the numbering to start at 0.
Thx to everyone.

Scriptage
06-05-2003, 06:12 AM
no problem