Connecting Tech Pros Worldwide Forums | Help | Site Map

Stopping URL from loading (from anchor)

saif
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi all,

I realy need your help.

I have a page with different web links, now what I am trying to do is
whenever a user clicks on a link that link is captured using
event.target method. I'm using this to process that URL, but the real
part is stopping the document from loading that URL. I tried stop()
method but its not working. I also tried asigning current URL of the
document to the document's location but it failed to work.

Is there any workaround to stop the document from loading a URL.

I don't want to change the anchors. I just want to use that workaround
or method after calling event.target() method.

Thanks.
Any help is appreciated.

Guido Wesdorp
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Stopping URL from loading (from anchor)


saif wrote:[color=blue]
> Hi all,
>
> I realy need your help.
>
> I have a page with different web links, now what I am trying to do is
> whenever a user clicks on a link that link is captured using
> event.target method. I'm using this to process that URL, but the real
> part is stopping the document from loading that URL. I tried stop()
> method but its not working. I also tried asigning current URL of the
> document to the document's location but it failed to work.
>
> Is there any workaround to stop the document from loading a URL.
>
> I don't want to change the anchors. I just want to use that workaround
> or method after calling event.target() method.
>
> Thanks.
> Any help is appreciated.[/color]

I guess what you want to do is use JavaScript to find all anchors in a
document (document.getElementsByTagName('a')) and put an onclick on
them, that does what you want it to do and also cancels the event
('event.preventDefault()' on Mozilla, 'event.returnValue = false' on IE).

Small example:

================================================== =====================

function process_link_click(event) {
var link = event.target ? event.target : event.srcElement;
var href = link.getAttribute('href');
// do something with your href here
if (event.preventDefault) {
// mozilla
event.preventDefault();
} else {
event.returnValue = false;
};
};

function find_and_patch_links() {
var links = document.getElementsByTagName('a');
for (var i=0; i < links; i++) {
if (link.addEventListener) {
// mozilla
link.addEventListener('click', process_link_click, false);
} else if (link.attachEvent) {
// IE
link.attachEvent('onclick', process_link_click);
};
};
};

================================================== =====================

You will have to make sure the find_and_patch_links() function is called
on load of the document (<body onload="find_and_patch_links()">). Note
that I haven't tested this code so it may contain a bug or two ;)

Cheers,

Guido
Michael Winter
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Stopping URL from loading (from anchor)


On Tue, 04 May 2004 11:37:52 +0200, Guido Wesdorp <guido@infrae.com> wrote:

[snip]
[color=blue]
> I guess what you want to do is use JavaScript to find all anchors in a
> document (document.getElementsByTagName('a'))[/color]

A more reliable approach is to use the document.links collection. It's
supported by more browsers.
[color=blue]
> and put an onclick on them, that does what you want it to do and also
> cancels the event ('event.preventDefault()' on Mozilla,
> 'event.returnValue = false' on IE).[/color]

If you use other approaches for attaching listeners (which I've added
below), returning false will suffice.

As we're attaching listeners directly to the A elements, one can simply
use the this operator to get a reference to the element.
[color=blue]
> function process_link_click(event) {
> var link = event.target ? event.target : event.srcElement;
> var href = link.getAttribute('href');[/color]

Unless you're working with XML, link.href will do and is more reliable.
[color=blue]
> // do something with your href here
> if (event.preventDefault) {
> // mozilla
> event.preventDefault();
> } else {
> event.returnValue = false;
> };
> };[/color]

By the way, you don't usually need to terminate blocks with a semi-colon.
The only exceptions I can think of at the moment are with the function
operator

ref = function() {
};

and the object literal

obj = {};

Notice that the difference is due to the assignment which, as a statement,
should be terminated.
[color=blue]
> function find_and_patch_links() {
> var links = document.getElementsByTagName('a');[/color]

var links = documents.links;

is more compatible.
[color=blue]
> for (var i=0; i < links; i++) {[/color]

That's an error. In any case, it's better to save the property value
rather than repeatedly looking it up:

for( var i = 0, n = links.length; i < n; ++i ) {
[color=blue]
> if (link.addEventListener) {[/color]

This is also an error as 'link' is undefined.
[color=blue]
> // mozilla
> link.addEventListener('click', process_link_click, false);
> } else if (link.attachEvent) {[/color]

Unfortunately, Microsoft's attachEvent() mechanism is flawed and doesn't
set the this operator correctly. As I'm adding support for older browsers
anyway, we'll skip attachEvent() and use the event properties with IE.
[color=blue]
> // IE
> link.attachEvent('onclick', process_link_click);
> };
> };
> };[/color]

The culmination of which produces:

function process_link_click( event ) {
var href = this.href;
event = event || window.event;

// do something with your href here
// ...

if( event.preventDefault ) {
event.preventDefault();
}
return false;
}

function find_and_patch_links() {
var links = document.links, link;

for( var i = 0, n = links.length; i < n; ++i ) {
link = links[ i ];

if( link.addEventListener ) {
link.addEventListener( 'click', process_link_click, false );
} else {
link.onclick = process_link_click;
}
}
}

Mike

--
Michael Winter
M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Shawn Milo
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Stopping URL from loading (from anchor)


I think that this is the easiest way to do it:

<a href="http://www.google.com">link1</a> will load<br/>
<a href="http://www.google.com" onclick="return false;">link2</a> will not load<br/>

Shawn
Guido Wesdorp
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Stopping URL from loading (from anchor)


Michael Winter wrote:[color=blue]
>
> The culmination of which produces:
>
> function process_link_click( event ) {
> var href = this.href;
> event = event || window.event;
>
> // do something with your href here
> // ...
>
> if( event.preventDefault ) {
> event.preventDefault();
> }
> return false;
> }
>[/color]
Are you sure 'return false;' is sufficient? I remember trying it but
with no avail, could be I was doing something wrong as well... I do know
it works when you define an event handler on an element (<a
onclick="return false;">) but not when using JavaScript to dynamically
attach them...
Anyway, thanks for fixing the bugs, and thanks for making it less
browser-specific. I write Mozilla/IE apps usually (check out Kupu,
kupu.oscom.org, for example), without having to care about older/other
browsers, so I don't know much about writing 'legacy' code...

Cheers,

Guido
Michael Winter
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Stopping URL from loading (from anchor)


On Tue, 04 May 2004 15:42:55 +0200, Guido Wesdorp <guido@infrae.com> wrote:
[color=blue]
> Michael Winter wrote:[color=green]
>> The culmination of which produces:
>>
>> function process_link_click( event ) {
>> var href = this.href;
>> event = event || window.event;
>>
>> // do something with your href here
>> // ...
>>
>> if( event.preventDefault ) {
>> event.preventDefault();
>> }
>> return false;
>> }[/color]
>
> Are you sure 'return false;' is sufficient? I remember trying it but
> with no avail, could be I was doing something wrong as well... I do know
> it works when you define an event handler on an element (<a
> onclick="return false;">) but not when using JavaScript to dynamically
> attach them...[/color]

Probably not with listeners added with addEventListener(), which is why I
left the preventDefault() call. Returning false isn't specified in DOM 2
Events, so it's probably not implemented and why should it? The legacy
argument doesn't apply there. However, returning false is sufficient for
the event properties as it is effectively the same as specifying the
intrinsic event in HTML. Using the returnValue property doesn't appear to
be necessary in IE with attachEvent() but it wouldn't hurt (I usually
avoid that mechanism like the plague, though).
[color=blue]
> Anyway, thanks for fixing the bugs, and thanks for making it less
> browser-specific. I write Mozilla/IE apps usually (check out Kupu,
> kupu.oscom.org, for example), without having to care about older/other
> browsers, so I don't know much about writing 'legacy' code...[/color]

You're welcome.

Mike

--
Michael Winter
M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
saif
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Stopping URL from loading (from anchor)


Thanks Wesdorp, your code snippet really helped me.

This is what I was looking for:

if (event.preventDefault) {
// mozilla
event.preventDefault();
} else {
event.returnValue = false;
};

Surprisingly I didn't find any 'preventDefault' discription in
JavaScript documentation from Netscape.

Thanks again.
Michael Winter
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Stopping URL from loading (from anchor)


On 4 May 2004 08:40:33 -0700, saif <unmssa@yahoo.com> wrote:
[color=blue]
> Thanks Wesdorp, your code snippet really helped me.
>
> This is what I was looking for:
>
> if (event.preventDefault) {
> // mozilla
> event.preventDefault();
> } else {
> event.returnValue = false;
> };
>
> Surprisingly I didn't find any 'preventDefault' discription in
> JavaScript documentation from Netscape.[/color]

That's because it's part of the World Wide Web Consortium's (W3C) Document
Object Model (DOM). Specifically, DOM Level 2 Events. The DOM home is here:

<URL:http://www.w3.org/DOM/>

The Technical Reports section contains the various specifications. It's
best to avoid Level 3 for now as most browsers don't implement it.

<URL:http://www.w3.org/DOM/DOMTR/>

Mike

--
Michael Winter
M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Guido Wesdorp
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Stopping URL from loading (from anchor)


Michael Winter wrote:[color=blue]
>
> <URL:http://www.w3.org/DOM/>
>
> <URL:http://www.w3.org/DOM/DOMTR/>
>[/color]
Mozilla's DOM documentation is pretty useful here as well:

http://www.mozilla.org/docs/dom/domref/

Cheers,

Guido
Closed Thread


Similar JavaScript / Ajax / DHTML bytes