/    Sign up×
Community /Pin to ProfileBookmark

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 ?
Any help is appreciated…

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJun 04.2003 — There are few things that you can do.

1. the out of memory is being caused because you are calling getLinks(number) function recursively..

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

<i> </i>};
<i> </i>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
  • Copy linkTweet thisAlerts:
    @brendandonhueauthorJun 05.2003 — 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? [code=php]
    <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> [/code]
    Copy linkTweet thisAlerts:
    @brendandonhueauthorJun 05.2003 — 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?
    Copy linkTweet thisAlerts:
    @Khalid_AliJun 05.2003 — 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.
    Copy linkTweet thisAlerts:
    @brendandonhueauthorJun 05.2003 — 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>
    Copy linkTweet thisAlerts:
    @Khalid_AliJun 05.2003 — 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?
    Copy linkTweet thisAlerts:
    @JHLJun 05.2003 — document.write() will overwrite everything in the document.

    so you should put everything in a variable first before using document.write().
    Copy linkTweet thisAlerts:
    @brendandonhueauthorJun 05.2003 — Yes khalid, thats what im trying to do.

    Thx JHL im working on it.
    Copy linkTweet thisAlerts:
    @ScriptageJun 05.2003 — 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).

    [code=php]
    <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
    [/code]
    Copy linkTweet thisAlerts:
    @brendandonhueauthorJun 05.2003 — Thanks scriptage, worked perfectly! I just edited it slightly because I wanted the numbering to start at 0.

    Thx to everyone.
    Copy linkTweet thisAlerts:
    @ScriptageJun 05.2003 — no problem
    ×

    Success!

    Help @brendandonhue spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...