I need to extract links from a given page to be displayed in a new window, however, the particular page im wanting to extract links from, I dont have direct access to that pages html.

this below code extracts all links on any page its on.....

<script language="JavaScript1.2">
<!--
/*
Link Extractor Script- 
*/
function extractlinks(){
var links=document.all.tags("A")
var total=links.length
var win2=window.open("","","menubar,scrollbars")
win2.document.write("<h2>Total Links="+total+"</h2><br>")
for (i=0;i<total-1;i++){
win2.document.write(links[i].outerHTML+"<br>")
}
}
//-->
</script>
<button onClick="extractlinks()">Extract Links</button>

......but could it be altered to extract from a given page via http://, even though its not on that page? im thinking something like

<center><script language="JavaScript1.2">
<!--

/*
Link Extractor Script- 
*/
function extractlinks(){
var links=document.all.http://("http://www.google.com")
var total=links.length
var win2=window.open("","","menubar,scrollbars")
win2.document.write("<h2>Total Links="+total+"</h2><br>")
for (i=0;i<total-1;i++){
win2.document.write(links[i].outerHTML+"<br>")
}
}
//-->
</script>
<button onClick="extractlinks()">Extract Links</button></center>

can javascript do that?

Recommended Answers

All 5 Replies

Javascript executes on the client machine. The thing you are trying to do involves server side interaction. Either fetch the entire Google Homepage as plain text using Ajax's 'responseText' or do it pure server side and generate the entire HTML on fly.

There are a lot of 'Eeek' things in your code. Keep in mind that 'document.write()' is evil. Also the language attribute of <script> tag is deprecated. Use <script type="text/javascript"></script> instead. document.all is IE only and not W3C compliant. Instead of that use document.getElementsByTagName("a") (notice the lowecase 'a'). And its ' onclick ' and not ' onClick '.

lol why is document.write evil?

I have updated my previous post with links, read those.

I can see two problems:

- Modifying code on the fly is always a source of hard-to-locate grief and megamanhours of debugging. Change the attributes of an object instead.

- You could be violating copyright by stealing information from someone else's website.

Its nothing to do with anyone elses website its for mine. I run 2 sites and have an iframe i use to display an updated listing of recent topics.
I want to allow members of each site to access/veiw the topics of the other. but the two sites are on different servers hence the problem.
the idea was that i would simply create and host a new page and use this code to extract links from it, so one site can access the others links.
both my sites, no copyright infringement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.