473,625 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'this' and <A HREF="...">

In my app I need to dynamically generate a series hyperlinks. Each
hyperlink's action must be to focus a field in a <form>. I created the
following function to create such a link (the argument is a field
object, e.g. <input>):

function createFieldAnch or(field, linkText)
{
var anchor = document.create Element("a");
anchor.field = field;
anchor.href='ja vascript:this.f ocusField()';
anchor.innerHTM L = linkText;
anchor.focusFie ld = function()
{
var f = this.field;
f.focus();
f.select();
}
return anchor;
}

The link does not work. Debugging shows that the problem is in
'javascript:thi s.focusField()' . The 'this' operator does not refer to
the anchor here (as I expected), but to the window object!

Is there a simple way to get a reference to the anchor in that
context?

I thought up the following 'tricky' solution, which works, but it's
not a very elegant solution:

//General function to implement auto-increments on an object.
function genAutoInc(obj)
{
if (obj.autoIncSee d == null) {
obj.autoIncSeed = 0;
}
obj.autoIncSeed ++;
return obj.autoIncSeed ;
}

//This version assigns a unique ID to the anchor and uses that to
refer to it in the HREF.
function createFieldAnch or2(field, linkText)
{
var anchor = document.create Element("a");
anchor.field = field;
anchor.id = 'dynamicId'+gen AutoInc(field.o wnerDocument);
anchor.href='ja vascript:docume nt.getElementBy Id("'+anchor.i d
+'").focusField ()';
anchor.innerHTM L = linkText;
anchor.focusFie ld = function()
{
var f = this.field;
f.focus();
f.select();
}
return anchor;
}
Jun 27 '08 #1
2 2003
Peter Laman schreef:
In my app I need to dynamically generate a series hyperlinks. Each
hyperlink's action must be to focus a field in a <form>. I created the
following function to create such a link (the argument is a field
object, e.g. <input>):

function createFieldAnch or(field, linkText)
{
var anchor = document.create Element("a");
anchor.field = field;
anchor.href='ja vascript:this.f ocusField()';
anchor.innerHTM L = linkText;
anchor.focusFie ld = function()
{
var f = this.field;
f.focus();
f.select();
}
return anchor;
}

The link does not work. Debugging shows that the problem is in
'javascript:thi s.focusField()' . The 'this' operator does not refer to
the anchor here (as I expected), but to the window object!

Is there a simple way to get a reference to the anchor in that
context?

I thought up the following 'tricky' solution, which works, but it's
not a very elegant solution:

//General function to implement auto-increments on an object.
function genAutoInc(obj)
{
if (obj.autoIncSee d == null) {
obj.autoIncSeed = 0;
}
obj.autoIncSeed ++;
return obj.autoIncSeed ;
}

//This version assigns a unique ID to the anchor and uses that to
refer to it in the HREF.
function createFieldAnch or2(field, linkText)
{
var anchor = document.create Element("a");
anchor.field = field;
anchor.id = 'dynamicId'+gen AutoInc(field.o wnerDocument);
anchor.href='ja vascript:docume nt.getElementBy Id("'+anchor.i d
+'").focusField ()';
anchor.innerHTM L = linkText;
anchor.focusFie ld = function()
{
var f = this.field;
f.focus();
f.select();
}
return anchor;
}
Hi,

I think you can avoid this whole mess by NOT using the javascript
pseudocode in your hyperlinks.
I think that is the reason your 'this' is the windowsobject.
Try this:
1) remove the anchortags completely
2) replace them by a span tag. (If you miss your hyperlinkcursor , use
the cursor CSS solution, eg style="cursor:c rosshair")
3) Give the span an onClick eventhandler.
4) add onClick() to the span and call some function with the right
parameters, eg onClick="focusM e('field1');"

function focusMe(fieldNa me){
document.forms["yourformnamehe re"][fieldName].focus();
}

Could that help?

Regards,
Erwin Moller
Jun 27 '08 #2
Peter Laman wrote:
In my app I need to dynamically generate a series hyperlinks. Each
hyperlink's action must be to focus a field in a <form>. I created the
following function to create such a link (the argument is a field
object, e.g. <input>):

function createFieldAnch or(field, linkText)
{
var anchor = document.create Element("a");
anchor.field = field;
anchor.href='ja vascript:this.f ocusField()';
anchor.innerHTM L = linkText;
anchor.focusFie ld = function()
{
var f = this.field;
f.focus();
f.select();
}
return anchor;
}

The link does not work. Debugging shows that the problem is in
'javascript:thi s.focusField()' .
http://jibbering.com/faq/#FAQ4_24
The 'this' operator
`this' is _not_ an operator.
does not refer to the anchor here (as I expected), but to the window object!
this, literally, is unsurprising. However, it does not necessarily refer to
the object referred to by the host-defined `window' property of the Global
Object, but rather to the Global Object itself.
Is there a simple way to get a reference to the anchor in that
context?
Yes, there is:

<a href="alternati ve" onclick="... this ... ">...</a>

However, your needs go further and your code points out a confusion between
host objects and native objects on your part. Trying to augment DOM host
objects with a property as you do is a really bad idea because of what the
ECMAScript Specification says about that and what implementations exhibit.

You really want to use a wrapper method instead:

function focusField(a)
{
// call another wrapper method for ensuring compatibility;
// see also http://PointedEars.de/scripts/dhtml.js
var f = dhtml.getElemBy Id(a.id + "_field");
if (f)
{
// add feature tests here
f.focus();
f.select();
}
}

Since you are using a method to create your special anchor already, it would
be prudent to make the focusField() method a property of a wrapper object:

function FieldAnchor(fie ld, linkText)
{
// the local variable keeps the value private;
// if you don't want that, use this.anchor and the like;
var anchor = dhtml.createEle ment("a");

this.field = field;

var me = this;

// call wrapper method for ensuring compatibility
_addEventListen er(anchor, "click",
function() {
me.focusField() ;
});

// feature-test this before, and avoid this;
// append a text node or several other node objects instead
anchor.innerHTM L = linkText;

// Allows "protected" read access to the "private" variable.
// (must be defined here and not as a prototype method)
this.getNode = function() {
return anchor;
};
}

FieldAnchor.pro totype = {
constructor: FieldAnchor,
focusField: function() {
var f = this.field;
if (f)
{
// add feature tests here
f.focus();
f.select();
}
}
};

// example; add more feature tests here
var a = new FieldAnchor(dht ml.getElemById( "foo"), "bar");
document.body.a ppendChild(a.ge tNode());
HTH

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #3

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

Similar topics

6
2585
by: lecichy | last post by:
Hello. Well, I found this piece of code on php.net. Thats fine but where can i find explanation for all these ("|\')?(*)("|\')?.*>(*)' syntax so that I can construct my own rules for all kind of eregi preg and oter match functions ?
2
8767
by: Charles Nadeau | last post by:
Hello, I am trying to craft a regular expression to filter an URL from a <a href=""></a> tag and the one I have doesn't seen right. I use the regular expression from this snippet of code: foreach my $message (@messages) { my @match=($message->decoded=~/\bhref="(http.*)">.*/gi);
1
6819
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
6
77500
by: Tony Marston | last post by:
The code <a href="..." target="_blank">...</a> will not validate as XHTML STRICT because of the 'target' tag, so how do I achieve the same result by moving it to a CSS file? I cannot find anything which allows me to specify 'target=' on an anchor tag. -- Tony Marston http://www.tonymarston.net
10
60700
by: Dieter Salath? | last post by:
Hi, in our webpage, a user could open a windows explorer to his temp directory with a simple link and usage of the file protocol: <a href="file://C:\temp" target="_blank">C:\temp</a> This worked very well a long time, but now it does not work anymore. We use IE6 and Microsoft Windows XP Professional 2002 SP2. I guess it has something to do with new IE security features. Does
3
1742
by: Oliver Saunders | last post by:
can someone please tell me why everytime i try and pass this to a js function i can't seem to use it for anything: <html> <head> <script language="javascript"> function a(fromlink) { alert(fromlink.nodeName); // outputs undefined alert(fromlink.nodeType); // outputs undefined alert(fromlink.parentNode); // outputs undefined
10
67748
by: Eric-Sebastien Lachance | last post by:
Hey there, I decided to just create a 100% height and width div that filled the space over a background header image, and add an onclick event to redirect to the my index... Doesn't seem to work in FireFox only, just in IE. Here's the code: <div id="headerimg"><div style="width: 100%; height: 100%; cursor: hand; cursor: pointer;" onclick="top.location.href('http://www.monamouchtim.com/');">&nbsp;</div></div>
3
1448
by: Jim Carlock | last post by:
In creating a dynamic page with some static content, ie, the list of city names is standard HTML encoding. When I click on the link, I see the page reload itself instead of jumping to the content where <a name="raleigh"></a>. Any suggestions on how to get around this? Specifically the page I'm working with... http://microcosmotalk.com/live/category/NC/Swimming_Pool_Designers/?area=Raleigh+NC&builder=Swimming+Pool+Builder
1
2422
by: mark4asp | last post by:
<form runat="server"automatically adds <divtag to code contained within. Is there a way to stop that? Mixing block-level elements with inline-level elements messes up the HTML becasuse that is invalid for a strict implementation. <spanis in-line-level and <divis block-level. I don't want to mix up <span> and <div> I'm using an <asp:Buttonhere because when it switches to the URL I need to check that the user is in the correct role -...
3
7820
by: joe | last post by:
Is it OK to have multiple: <script type="text/javascript" src="funcs1.js"></script> <script type="text/javascript" src="funcs2.js"></script> <script type="text/javascript" src="funcs3.js"></script> ? And I need to use similarly multiple CSS:
0
8182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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...
0
7178
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
6115
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
5570
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
4085
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...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
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.