Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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" );
    Last edited by Old Pedant; 07-31-2010 at 12:55 AM.
    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.

  • Users who have thanked Old Pedant for this post:

    dntel123 (07-31-2010)

  • #4
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •