473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

picking up URL when link is clicked

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
9 1939
* 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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
1
by: Mat | last post by:
How can I detect when a link has been clicked but the new page is still in the process of loading? The document.location.href property still displays the current location (understandably) not the...
4
by: Newbie | last post by:
Is it possible to set up an event handler or something else so that when *any* link on the page is clicked it 'fires-up', executes some JS and then continues to process the link that was clicked?...
6
by: Tom Braun | last post by:
Hello! My first post here... I need to monitor if someone clicked on some (any) link in a subframe. Due to certain restrictions, the only place I can put some JavaScript is in the main...
7
by: who be dat? | last post by:
I need some help here. I'm creating a list on a page where the list is created from a dataset with two tables linked with a datarelation. The first table is a list of groups while the second...
23
by: wylbur37 | last post by:
I'm running an Apache server on my own computer (Windows XP Pro). I wrote a simple PHP script (called test3.php) that I'm running by putting the following URL in the address bar of the browser...
6
by: hemant.singh | last post by:
Hi all, I am trying to get a way by which I'll know exactly when user goes out of my site by clicking on close button in browser, So that w/e user click close button in browser, I can send a...
10
by: arunsenthil | last post by:
Hi, Iam new to JS. Kindly help me to solve the following difficulty. Consider 3 links such as link1,link2,link3 in gray color at top of the page. When each link is clicked the corresponding...
3
by: jeddiki | last post by:
Hi, I am using this script which is nearly working correctly, but not quite! When a user selects some text from the web-page and copies it, the script is supposed to pick up the current...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.