By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

picking up URL when link is clicked

P: n/a
Hello everyone :-) ,
Thanks to the gentle people on this group
for helping me out with previous issues. :-D This time round I am
facing what I perceive as a simple problem, which I have not found a
simple solution for (obviously!).

The problem: I need to pick up the URL in the address bar of a browser
when a link is clicked

What I have done: I have used,
a function which routes document.onclick into a func which uses
window.content.document.location.href to pick up the URL from the
address bar.

The issue: when I click link on the page the alert box displays the
URL in the address bar, not the address that I am planning to go to
when I click on the link. I tried to put in a delay before the address
bar content is picked up. I used setTimeout, a loop function but still
these only delay the picking up of the current URL in the address bar.
I thought that if I put in a delay, when I click a link, the URL in
the address bar would change and after 500 ms I could easily pick up
the destination URL.

Is there an elegant way to accomplish this? any pointers/comments/code
are very appreciated :-) . I have searched this group and have not
found an exact solution. Do I have to take control of the HTTP
channel? Also, If I want to block access to the link, providing the
user with some info before he/she actually views the page, how could I
do it? Any pointers?

Thanks in advance :-)
Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
* 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 <aelements check the href attribute. That's imperfect,
but there is no other way currently.
--
Björn Höhrmann · mailto:bj****@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/
Jun 27 '08 #2

P: n/a
On Apr 14, 12:22 pm, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:
* 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 <aelements 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 <atags 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.

Thanks again.

function extractlinks(e){

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>")

}

}
document.onload = extractlinks;

function kC(e) {
alert(window.content.document.location.href);

}
document.onclick = kC;

Jun 27 '08 #3

P: n/a
newbiegalore wrote:
function extractlinks(e){

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>")

}

}
window.onload = extractlinks;
function extractlinks(){
var links=document.links
var total=links.length
var win2=window.open("","","menubar,scrollbars")
win2.document.write("<h2>Total Links="+total+"</h2><br>")

for (var i=0;i<total;i++){
win2.document.write(links[i].parentNode.innerHTML+"<br>")
}
}
Mick
>
document.onload = extractlinks;

function kC(e) {
alert(window.content.document.location.href);

}
document.onclick = kC;
Jun 27 '08 #4

P: n/a
On Apr 14, 1:40 pm, Michael White <m...@me.comwrote:
newbiegalore wrote:
function extractlinks(e){
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>")
}
}

window.onload = extractlinks;

function extractlinks(){
var links=document.links
var total=links.length
var win2=window.open("","","menubar,scrollbars")
win2.document.write("<h2>Total Links="+total+"</h2><br>")

for (var i=0;i<total;i++){
win2.document.write(links[i].parentNode.innerHTML+"<br>")

}
}

Mick
document.onload = extractlinks;
function kC(e) {
alert(window.content.document.location.href);
}
document.onclick = kC;
Hi Mick, thanks for the code correction, but when I tried it the
browser just seemed to stop! did not load home page. I also tried
something simpler,

function extractlinks(e){
var obj=document.getElementsByTagName('a')
for(i=0;i<obj.length;i++)
alert(obj[i].parentNode.innerHTML)
}

window.onload = extractlinks;

this too does not seem to work... hmmm, I'll keep hacking on it.
PS: I have tried using window instead of document and it did not seem
to make a difference.
Jun 27 '08 #5

P: n/a
newbiegalore wrote:
>
Hi Mick, thanks for the code correction, but when I tried it the
browser just seemed to stop! did not load home page. I also tried
something simpler,

function extractlinks(e){
var obj=document.getElementsByTagName('a')
for(i=0;i<obj.length;i++)
alert(obj[i].parentNode.innerHTML)
}

window.onload = extractlinks;

this too does not seem to work... hmmm, I'll keep hacking on it.
PS: I have tried using window instead of document and it did not seem
to make a difference.
Works for me. What error are you getting? Why the "e" parameter?
Mick
Jun 27 '08 #6

P: n/a
On Apr 14, 4:23 pm, Michael White <m...@me.comwrote:
newbiegalore wrote:
Hi Mick, thanks for the code correction, but when I tried it the
browser just seemed to stop! did not load home page. I also tried
something simpler,
function extractlinks(e){
var obj=document.getElementsByTagName('a')
for(i=0;i<obj.length;i++)
alert(obj[i].parentNode.innerHTML)
}
window.onload = extractlinks;
this too does not seem to work... hmmm, I'll keep hacking on it.
PS: I have tried using window instead of document and it did not seem
to make a difference.

Works for me. What error are you getting? Why the "e" parameter?
Mick
OK here's what happened. When the whole code is executed, all I see is
a white screen and firefox does not respond, there is no new window,
no alert box.

When I run just

alert('hello') within the extractlinks function, the first time I run
firefox, nothing happens but from the second time everything works.

when I use just,

var links=document.links
var total=links.length
alert(total)

again firefox gets kinda hosed and white screens are all I get, the
homepage does not open. If any specific details would help please let
me know. Thanks again for your time.

I am using Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:
1.8.1.13) Gecko/20080311 Firefox/2.0.0.13
Jun 27 '08 #7

P: n/a
On Apr 15, 5:42 am, newbiegalore <banerjee.anir...@gmail.comwrote:
On Apr 14, 12:22 pm, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:
* 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 <aelements 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 <atags 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
Jun 27 '08 #8

P: n/a
On Apr 14, 8:04*pm, RobG <rg...@iinet.net.auwrote:
On Apr 15, 5:42 am, newbiegalore <banerjee.anir...@gmail.comwrote:
On Apr 14, 12:22 pm, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:
* 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 <aelements 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 <atags 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-HTML/html.html#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
Guys, thanks a ton for the code comments and contribution. The thing
is your code works perfect when I run it as a standalone webpage. When
I run it by placing it in a .js file which is linked to the firefox
extension toolbar I am developing it just does not work. Only rally
rudimentary stuff like once click is pressed an alert pops up showing
the address bar url or a msg works. It just won't pick up anything
else. In fact, even when I try to just print using alert the var i =
document.links.length and I set the default homepage to digg.com which
has a large number of links on the page, it still won't do anything.

Again your code works great when it is present in the body of a
webpage. Thanks :-) . However, I am trying to use the input from you
guys to try and make the js work for my toolbar.

I have tried using (document/window).on(click/load) = funcname;
function funcname(){

var i = document.links.length;
alert(i);
}

does not do anything! I have checked up on the MDC and the code you
guys have helped with matches with standards but still it just won't
do anything! arrrrrgh!
Jun 27 '08 #9

P: n/a
On Apr 14, 11:35*pm, newbiegalore <banerjee.anir...@gmail.comwrote:
On Apr 14, 8:04*pm, RobG <rg...@iinet.net.auwrote:
On Apr 15, 5:42 am, newbiegalore <banerjee.anir...@gmail.comwrote:
On Apr 14, 12:22 pm, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:
* 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 <aelements 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 <atags 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-HTML/html.html#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

Guys, thanks a ton for the code comments and contribution. The thing
is your code works perfect when I run it as a standalone webpage. When
I run it by placing it in a .js file which is linked to the firefox
extension toolbar I am developing it just does not work. Only rally
rudimentary stuff like once click is pressed an alert pops up showing
the address bar url or a msg works. It just won't pick up anything
else. In fact, even when I try to just print using alert the var i =
document.links.length and I set the default homepage to digg.com which
has a large number of links on the page, it still won't do anything.

Again your code works great when it is present in the body of a
webpage. Thanks :-) . However, I am trying to use the input from you
guys to try and make the js work for my toolbar.

I have tried using (document/window).on(click/load) = funcname;
function funcname(){

*var i = document.links.length;
*alert(i);

}

does not do anything! I have checked up on the MDC and the code you
guys have helped with matches with standards but still it just won't
do anything! arrrrrgh!
The problem has ben resolved, more info at
http://forums.mozillazine.org/viewto...339707#3339707
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.