Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-31-2010, 12:08 AM   PM User | #1
dntel123
New to the CF scene

 
Join Date: Jul 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
dntel123 is an unknown quantity at this point
Adding a Break to my Loop

Hiya

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):

Link
Link

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

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

Code:
<HTML>
 <HEAD>
  <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++)
		
			{

			window.open(a[i].href);
			var divReport = document.getElementById("report");
			report.innerHTML = (a[i].href);
			document.report.write(item );
			document.write("<br\/>");
			
			
			}
	}
	-->
</script>
 </HEAD>

 <BODY>
  
<div>
	<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>

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

 </BODY>
</HTML>
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!

Liam
dntel123 is offline   Reply With Quote
Old 07-31-2010, 12:49 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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:
Code:
         document.report.write(item );
since item isn't defined anywhere.

*PROBABLY* all you need to do is replace these three lines:
Code:
			report.innerHTML = (a[i].href);
			document.report.write(item );
			document.write("<br\/>");
with this:
Code:
			report.innerHTML +=  a[i].href + "<br/>";
But untested.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 07-31-2010, 12:52 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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:
Code:
          window.open( a[i].href, "_blank" );
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 07-31-2010 at 12:55 AM..
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
dntel123 (07-31-2010)
Old 07-31-2010, 11:31 AM   PM User | #4
dntel123
New to the CF scene

 
Join Date: Jul 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
dntel123 is an unknown quantity at this point
Thanks,

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.

Liam
dntel123 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:24 PM.


Advertisement
Log in to turn off these ads.