On Apr 15, 5:42 am, newbiegalore <banerjee.anir...@gmail.com> wrote:
> On Apr 14, 12:22 pm, Bjoern Hoehrmann <bjo...@hoehrmann.de> wrote:
>
>
>
> > * newbiegalore wrote in comp.lang.javascript:
>
> > >The problem: I need to pick up the URL in the address bar of a browser
> > >when a link is clicked
>
> > As I understand you, you want to know the address after it has changed.
> > The problem is that unless you have document internal links, it will
> > change only after the current document is unloaded -- and all scripts
> > running in the context of that document terminated. That's not possible,
> > instead you have to check which link is clicked and construct the URL
> > based on this information. You have to check the target of the event
> > and, say, for <a> elements check the href attribute. That's imperfect,
> > but there is no other way currently.
> > --
> > Björn Höhrmann · mailto:bjo...@hoehrmann.de ·http://bjoern.hoehrmann.de
> > Weinh. Str. 22 · Telefon: +49(0)621/4309674 ·http://www.bjoernsworld..de
> > 68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 ·http://www.websitedev.de/
>
> Hello
, thanks for the pointer. I used the following code, which
> should parse the <a> tags in the current page but unfortunately does
> not seem to list them in a new window! If you know of any resource
> which discusses link parsing or finding which link was clicked could
> you please point me to it.
You can either put an onclick handler on each link, or one higher up
the document tree to catch clicks on links. Here's a simple example
based on the first suggestion:
<title>Links</title>
<script type="text/javascript">
function showHref(e) {
alert(this.tagName + ': ' + this.href);
return false;
}
function init(){
var i = document.links.length;
while (i--) {
document.links[i].onclick = showHref;
}
}
</script>
<body>
<a href="foo.html">foo</a><br>
<a href="bar.html">bar</a><br>
<script type="text/javascript">init && init();</script>
</body>
> Thanks again.
>
> function extractlinks(e){
>
> var links=document.all.tags("A")
A elements are not necessarily links, they may also be anchors. Also,
don't use the IE proprietary document.all, use W3C standards.
var links = document.links;
<URL:
http://www.w3.org/TR/DOM-Level-2-HTM...tml#ID-7068919 >
> var total=links.length
> var win2=window.open("","","menubar,scrollbars")
> win2.document.write("<h2>Total Links="+total+"</h2><br>")
Most browsers will block pop-ups by default so it's not a good idea to
rely on them.
> for (i=0;i<total-1;i++){
Keep variables, particularly counters, local using the var keyword. I
also don't understand why you want total-1 since that will skip the
last link;
for (var i=0; i<total; i++) {
>
> win2.document.write(links[i].outerHTML+"<br>")
Don't use the IE proprietary outerHTML unless this is only for IE. It
is also better to construct a string of the HTML you wish to write,
then write it using a single document.write statement, consider
something like:
var link;
var html = [];
for (var i=0; i<total; i++) {
link = links[i];
html.push('<a href="' + link.href + '">'
+ link.innerHTML + '<\/a>';
}
// Use a single write
win2.document.write('<title>Links<\/title><h2>Total Links='
+ total + '<\/h2>' + html.join('<br>'));
// Don't forget to close the document
win2.document.close();
> }
>
> }
>
> document.onload = extractlinks;
Add onload handlers using window.onload, or as <body onload="...">, or
simply run from a script just before the end of the body element.
--
Rob