View Full Version : Adding a Break to my Loop

07-31-2010, 01:08 AM
Hiya :D

I have created a small html with some javascript that runs through and picks up <a hrefs> and opens the links in new tabs (to check raw html for dead links)

It also pastes the links on the same page, thing is, in my loop as you'll see below it pastes the link then I try to <br /> to display the next underneath like so (on the same page):


Basically it works perfect for link 1 but it doesnt loop back around and grab the 2nd link.

Ideally I would like numbered links with a total at the bottom, I've been sitting trying to do it for about 3 hours now :thumbsup:

Heres my code, will explain much faster if you run it:

<TITLE> New Document </TITLE>
<script type="text/javascript">
function open_links()
document.getElementById("htmlDiv").innerHTML = document.getElementById("htmlArea").value;
a = document.getElementById("htmlDiv").getElementsByTagName("a");

for(i=0; i < a.length; i++)


var divReport = document.getElementById("report");
report.innerHTML = (a[i].href);
document.report.write(item );



<textarea id="htmlArea" cols=50 rows=20></textarea>
<button onclick="open_links();">Check Links</button>

<div style="font-family: Lucida Console; color: rgb(255, 255, 255); font-size: 12px; background: none repeat scroll 0% 0% black; width: 523px;" id="report">


<div id="htmlDiv" style="display: none;"></div>


If anyone has an idea why it wont run I'd be greatful, because what I can do is make it work fine but it makes a new page, I want it on the same page below the text area :)

Thanks in advance for any help!


Old Pedant
07-31-2010, 01:49 AM
Because you are doing document.write().

When you call document.write *after* the page is loaded (basically, any time after <body onload=...> would trigger the onload), you will *COMPLETELY WIPE OUT* your HTML page, *INCLUDING* all the JavaScript on it!

document.write() can *ONLY* be used while the page is being created! Period!

I'm utterly mystified as to what you thought this line would do:

document.report.write(item );
since item isn't defined anywhere.

*PROBABLY* all you need to do is replace these three lines:

report.innerHTML = (a[i].href);
document.report.write(item );

with this:

report.innerHTML += a[i].href + "<br/>";

But untested.

Old Pedant
07-31-2010, 01:52 AM
I'm not clear, at all, how you think that this will "check for dead links".

If a link is dead, your window.open(a[i].href);
will simply open up a blank window, or one with a 404 Not Found error.

Come to think of it, on top of all that, the window.open() should *replace* your current window contents, not open them in a new tab/window. That part, at least, is easy to fix. Just add a window designator:

window.open( a[i].href, "_blank" );

07-31-2010, 12:31 PM

That works actually and keeps the paste on the same page. Thanks for the info about document write only working on the same page, I was not aware.

I know my code isn't the best or very logical but I dont know javascript, only started yesterday, just someone at work asked me to help out.

We have a campaign team and they have to do internal testing to check the emails have all the correct links, hence why I wanted to paste them (compare those to a spreadsheet) and open them (to make sure they all are valid links)

I am trying to make it better, but what you said has made me realise that if there is a blank link, it wont write anything to the page.

Thanks for your help anyway mate, I'll just keep trying to make it better and more logical.


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum