Show all Anchors in a file

[Total: 0    Average: 0/5]

This shows how to use JavaScript to search through an HTML file and find all the anchors (links) to other web pages.

Head Code:

<script type="text/javascript">
listanchors.nav=true
if (navigator.appName.indexOf("Miscrosoft") != -1) listanchors.nav=false
function listanchors(d) {
  var newwin=window.open("","navwin","menubar=yes,scrollbars=yes,resizable=yes,width=600,height=300")
  newwin.document.writeln("<b>Navigation Window:<br>"+d.title+"</b>")
  newwin.document.writeln("<BR>Number of named anchors="+d.anchors.length+"<br><BR>")
  for(var i=0;i<d.anchors.length;i++) {
    var text
	var a=d.anchors[i]
	if (listanchors.nav) text=a.text
	else text=a.innerText
	if ((text==null) || (text=='')) text=a.name
	newwin.document.write('<a href="#'+a.name+'"'+' onClick="opener.location.hash='' 
   + a.name + ''; return false;">')
   newwin.document.write(text)
   newwin.document.writeln("</a></br>")
  }
  newwin.document.close()
}
defaultStatus="Click the Button!"
</script>

Body Code:

<a href="http://www.js-examples.com/" onMouseOver="status='No Name For This One';return true;">To the first link</a>
<br>
<a name="Num2" onMouseOver="status='Name=Num2';return true;" href="http://www.js-examples.com/">To the second one</a>
<br>
<a name="a_name" onMouseOver="status='Name=a_name';return true" href="http://www.js-examples.com/">Third Choice</a>
<BR>There is fourth link here --->
<a name="lastA" onMouseOver="status='Name=lastA';return true" href="http://www.js-examples.com/"></a>
<--- that you can not see
<BR><BR>
<form>
<input type="button" value="Click to begin the search" onClick="listanchors(document)">
</form>

Times Viewed: 1

Leave a Reply

Your email address will not be published. Required fields are marked *