473,804 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stopping URL from loading (from anchor)

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.
Jul 23 '05 #1
8 1995
saif wrote:
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.


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

Small example:

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

function process_link_cl ick(event) {
var link = event.target ? event.target : event.srcElemen t;
var href = link.getAttribu te('href');
// do something with your href here
if (event.preventD efault) {
// mozilla
event.preventDe fault();
} else {
event.returnVal ue = false;
};
};

function find_and_patch_ links() {
var links = document.getEle mentsByTagName( 'a');
for (var i=0; i < links; i++) {
if (link.addEventL istener) {
// mozilla
link.addEventLi stener('click', process_link_cl ick, false);
} else if (link.attachEve nt) {
// IE
link.attachEven t('onclick', process_link_cl ick);
};
};
};

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

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

Cheers,

Guido
Jul 23 '05 #2
On Tue, 04 May 2004 11:37:52 +0200, Guido Wesdorp <gu***@infrae.c om> wrote:

[snip]
I guess what you want to do is use JavaScript to find all anchors in a
document (document.getEl ementsByTagName ('a'))
A more reliable approach is to use the document.links collection. It's
supported by more browsers.
and put an onclick on them, that does what you want it to do and also
cancels the event ('event.prevent Default()' on Mozilla,
'event.returnVa lue = false' on IE).
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.
function process_link_cl ick(event) {
var link = event.target ? event.target : event.srcElemen t;
var href = link.getAttribu te('href');
Unless you're working with XML, link.href will do and is more reliable.
// do something with your href here
if (event.preventD efault) {
// mozilla
event.preventDe fault();
} else {
event.returnVal ue = false;
};
};
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.
function find_and_patch_ links() {
var links = document.getEle mentsByTagName( 'a');
var links = documents.links ;

is more compatible.
for (var i=0; i < links; i++) {
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 ) {
if (link.addEventL istener) {
This is also an error as 'link' is undefined.
// mozilla
link.addEventLi stener('click', process_link_cl ick, false);
} else if (link.attachEve nt) {
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.
// IE
link.attachEven t('onclick', process_link_cl ick);
};
};
};


The culmination of which produces:

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

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

if( event.preventDe fault ) {
event.preventDe fault();
}
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.addEventLi stener ) {
link.addEventLi stener( 'click', process_link_cl ick, false );
} else {
link.onclick = process_link_cl ick;
}
}
}

Mike

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #3
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
Jul 23 '05 #4
Michael Winter wrote:

The culmination of which produces:

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

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

if( event.preventDe fault ) {
event.preventDe fault();
}
return false;
}

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
Jul 23 '05 #5
On Tue, 04 May 2004 15:42:55 +0200, Guido Wesdorp <gu***@infrae.c om> wrote:
Michael Winter wrote:
The culmination of which produces:

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

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

if( event.preventDe fault ) {
event.preventDe fault();
}
return false;
}
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...


Probably not with listeners added with addEventListene r(), 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).
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...


You're welcome.

Mike

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #6
Thanks Wesdorp, your code snippet really helped me.

This is what I was looking for:

if (event.preventD efault) {
// mozilla
event.preventDe fault();
} else {
event.returnVal ue = false;
};

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

Thanks again.
Jul 23 '05 #7
On 4 May 2004 08:40:33 -0700, saif <un****@yahoo.c om> wrote:
Thanks Wesdorp, your code snippet really helped me.

This is what I was looking for:

if (event.preventD efault) {
// mozilla
event.preventDe fault();
} else {
event.returnVal ue = false;
};

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


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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #8
Michael Winter wrote:

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

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

Mozilla's DOM documentation is pretty useful here as well:

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

Cheers,

Guido
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
25857
by: elsenraat_76 | last post by:
Hello! I was wondering if someone could help me out with a problem I'm having? I'm trying to input a javascript value into an anchor tag (from a function), but don't have an event to call the function...if that makes sense. Here's what I mean... I have a javascript array set up like so: <script language="javascript" TYPE="text/JavaScript"> //custom object constructor
4
4038
by: Kathryn | last post by:
Hello, When I try to load this xml file (see below) into an asp.net dataset using the following code: ds.ReadXml("xmlfilepath\xmlfile.xml", XmlReadMode.Auto) I get the following error: "The same table (description) cannot be the child table in two nested
0
900
by: Kathryn | last post by:
Hello, When I try to load this xml file (see below) into an asp.net dataset using the following code: ds.ReadXml("xmlfilepath\xmlfile.xml", XmlReadMode.Auto) I get the following error: "The same table (description) cannot be the child table in two nested
5
1469
by: Erol | last post by:
How do I get a type from a string? I'm retrieving a string value from my database so that I can set my property values dynamically. In the event "Form1_Load", you will see that I'm trying to set the button's anchor property to "System.Windows.Forms.AnchorStyles.Bottom" dynamically from a string (which I commented out). How can I get this code to work. I also started to use the variable "fi", but I don't know how to put it to good use....
4
1853
by: Nick Wedd | last post by:
I have a test page http://www.files.maproom.org/00/12/q/w.html which almost does what I want. The idea is that, when you hover the mouse over one of the dates near the top of the page, it updates _both_ the appearance of the map, _and_ the text appearing in the iframes further down the page. I would like it to do this smoothly; but each time you select a new page, the browser window jerks down to the iframes (where the text was...
3
3878
by: gary | last post by:
Hi, I am trying to reference an anchor in a user control with a url. This worked in 1.1 but no longer works in 2.0. The ascx control is located in a "/include" folder If you have a hyperlink control and you assign the navigateurl property = "../#anchor" whereby you want to add this # reference to the current url the url ends up with the #anchor twice.
9
3230
by: monomaniac21 | last post by:
hi all i want to use hyperlinks to 'load' content by changing the display of div tags. the problem i have is that unless i specify a href the anchor does not change the mouse pointer on hover even if display is set to block. it only look changes when there is a href there. but if i have a href there then when i click it will load the page, which i dont want. how can i get the anchor to look like a proper link where the users pointer...
4
6369
by: Jeff | last post by:
Hi, I'd like to write Javascript that stops animated gifs from animating. On Firefox, at least, window.stop(); does the trick, although it stops everything on the page and is kind of unpredictable. If I connect it to the onload event, sometimes only half the page will be displayed. Does the onload even fire before rendering? Does anyone know a reasonable way to accomplish my original goal of
3
4526
by: Joseph Gruber | last post by:
Hi all -- I have two questions. First, I'm adding a control to my form at runtime and the control/form seems to ignore the anchor property of the runtime control. Any idea how I can get a runtime control to anchor properly? Second -- the program I'm writting is supposed to look like a dos application (long story). When the application is "Normal" aka a small window then everything looks great. But when I resize the window to...
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10353
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.