Questions | Login

Top / Javascript

I need Javascript code to extract links?

demo 21 Dec 2008 03:11pm
Give me a javascript code that can extract links on a web page. Further the script should write those links to a blank div of the same page (when a button is clicked). I need help.

http://cheaphostindia.info
http://techbongo.com

Answers

demo 21 Dec 2008 05:38pm
You didn't say what exactly you wanted to write, but you should be able to get anything you need from the following example.

<head>
<script type="text/javascript">
function showLinks() {
// get all anchors in page
var links = document.getElementsByTagName( 'A' );
// how many anchors
var last = links.length;
// initialize output for concatenation
var list = '';
// for each anchor...
for (var i = 0; i < last; i++) {
// concatenate info for current anchor
list += 'link ' + (i + 1) + ': ' +
' href=' + links[i].href +
' - text=' + links[i].innerHTML + '<br>';
}
// get reference to out display area
var linksList = document.getElementById( 'linksList' );
// write output to page
linksList.innerHTML = list;
}
</script>
</head>
<body>
<form>
<input type="button"
value="show links"
onclick="showLinks()" />
</form>
<p>
<!-- put some links into page for dem purposes -->
<a href = "http://www.google.com"> Google</a> <br>
<a href = "http://www.wikipedia.com"> Wikipedia </a><br>
<a href = "http://www.yahoo.com"> Yahoo! </a><br>
</p>
<!-- output will go here -->
<div id="linksList"></div>
</body>
</html>

I used the deprecated form of the break tag to prevent Y!A from replacing it with actual breaks.

Related

Submit Answer

You must be logged in to post an answer. Please Login or Register.

Powered by phpMyAnswers.