471,091 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

string vs object

If I have:

<a href="javascript:myfunct('cool_links')" id="cool_links">

and I refer to it as:
function myfunct(mylink)
{
document.getElementById[mylink]
}

then it is NOT reffered to as an object. I have to change my link to :
<a href="javascript:myfunct(cool_links)" id="cool_links">

Notice I took the tick marks out. I'm not telling u anything new here. The
question I have is how can I include the link id as:
<a href="javascript:myfunct('cool_links')" id="cool_links"> with the tick
marks and then refer to it as an object?

Mike
Jul 20 '05 #1
7 2144
Ivo

"Michael Hill" <hi****@charter.net> wrote in message
news:vn************@corp.supernews.com...
If I have:

<a href="javascript:myfunct('cool_links')" id="cool_links">
Try this:
<a href="noscript.html" onclick="myfunct(this);return false"
id="cool_links">

See the word "this" (without quotes)? This is a reserved word in javascript,
referring to whatever element it finds itself in, in this case an <a> tag.
This will be interpreted as an object variable by the same function that you
wrote.
Notice also that using the javascript: protocol in href attribute is not a
good idea as it is not well supported by browsers. The href should be a
valid link to a document for script-impaired users. The onclick will handle
the javascript (no need for setting the protocol there), and if it return
false the href will be ignored.
Ivo

and I refer to it as:
function myfunct(mylink)
{
document.getElementById[mylink]
}

then it is NOT reffered to as an object. I have to change my link to :
<a href="javascript:myfunct(cool_links)" id="cool_links">

Notice I took the tick marks out. I'm not telling u anything new here. The
question I have is how can I include the link id as:
<a href="javascript:myfunct('cool_links')" id="cool_links"> with the tick
marks and then refer to it as an object?

Mike

Jul 20 '05 #2
yes the myfunct(this) is the same as if I used myfunct(cool_links).

Reason that I like somehow to store the string is that I created a hidden
variable like:
<input type='hidden' name='myhidden' value=''>

and would like to store the name of the object there so I can use it later
in javascript processing. If not I have to pass the object around through
each javascript function.

I dont think I can store the object there can I?

Mike
"Ivo" <no@thank.you> wrote in message
news:3f*********************@news.wanadoo.nl...

"Michael Hill" <hi****@charter.net> wrote in message
news:vn************@corp.supernews.com...
If I have:

<a href="javascript:myfunct('cool_links')" id="cool_links">
Try this:
<a href="noscript.html" onclick="myfunct(this);return false"
id="cool_links">

See the word "this" (without quotes)? This is a reserved word in

javascript, referring to whatever element it finds itself in, in this case an <a> tag.
This will be interpreted as an object variable by the same function that you wrote.
Notice also that using the javascript: protocol in href attribute is not a
good idea as it is not well supported by browsers. The href should be a
valid link to a document for script-impaired users. The onclick will handle the javascript (no need for setting the protocol there), and if it return
false the href will be ignored.
Ivo

and I refer to it as:
function myfunct(mylink)
{
document.getElementById[mylink]
}

then it is NOT reffered to as an object. I have to change my link to :
<a href="javascript:myfunct(cool_links)" id="cool_links">

Notice I took the tick marks out. I'm not telling u anything new here. The question I have is how can I include the link id as:
<a href="javascript:myfunct('cool_links')" id="cool_links"> with the tick marks and then refer to it as an object?

Mike


Jul 20 '05 #3
Ivo
"Michael Hill" <hi****@charter.net> wrote in message
news:vn************@corp.supernews.com...
"Ivo" <no@thank.you> wrote in message
news:3f*********************@news.wanadoo.nl...

"Michael Hill" <hi****@charter.net> wrote in message
news:vn************@corp.supernews.com...
If I have:

<a href="javascript:myfunct('cool_links')" id="cool_links">
and I refer to it as:
function myfunct(mylink)
{
document.getElementById[mylink]
}

The question I have is how can I include the link id as:
<a href="javascript:myfunct('cool_links')" id="cool_links">
with the tickmarks and then refer to it as an object?

Mike


Try this:
<a href="noscript.html" onclick="myfunct(this);return false"
id="cool_links">

See the word "this" (without quotes)? This is a reserved word in

javascript,
referring to whatever element it finds itself in, in this case an <a> tag. This will be interpreted as an object variable by the same function that

you
wrote.
Notice also that using the javascript: protocol in href attribute is not a good idea as it is not well supported by browsers. The href should be a
valid link to a document for script-impaired users. The onclick will

handle
the javascript (no need for setting the protocol there), and if it return false the href will be ignored.
Ivo

yes the myfunct(this) is the same as if I used myfunct(cool_links).

Reason that I like somehow to store the string is that I created a hidden
variable like:
<input type='hidden' name='myhidden' value=''>

and would like to store the name of the object there so I can use it later
in javascript processing. If not I have to pass the object around through
each javascript function.

I dont think I can store the object there can I?

Mike


Please put your reply at the bottom. The Usenet convention is to have the
answer follow the question, like in real life.

The variable this.id can be passed around separately if you find the object
too clumsy. I tried the above <a> tag with this function myfunct(mylink) {
alert(mylink); // [object]
alert(typeof mylink); // object
alert(mylink.id); // cool_links
alert(typeof mylink.id); // string
}

Btw, if the only purpose of the hidden input field is to store a string, why
not use a global variable? Global variables can even hold whole objects, so
you can check its offsetHeight or whatever you like whenever you like.
Ivo
Jul 20 '05 #4
"Michael Hill" <hi****@charter.net> wrote in message
news:vn************@corp.supernews.com...
If I have:

<a href="javascript:myfunct('cool_links')" id="cool_links">
<quote cite="http://jibbering.com/faq/#FAQ4_24">
I have <a href="javascript:somefunction()"> what ... ?
</quote>
and I refer to it as:
function myfunct(mylink)
{
document.getElementById[mylink]

<snip>

JavaScript uses square brackets in bracket notation property accessors,
for indexing arrays and to define array literals. Parentheses are used
for function calls. As you have it, this line of code is attempting to
read a property of the document.getElementById function. Because
JavaScript functions are objects this is feasible but the
document.getElementById function will not normally have a property with
a name that corresponds with the toString value of the myLink parameter
of the myfunct function.

Try:-
document.getElementById(mylink);
- instead. However, this function does not do anything with the result
of the function call so there is probably no point in calling the
function at all.

Note that getElementById retrieves references to DOM elements with the
corresponding ID attribute. Elements that only have NAME attributes must
be accessed by other means.

Richard.
Jul 20 '05 #5
[75 lines of quotation skipped]
Please put your reply at the bottom. The Usenet convention is to have the
answer follow the question, like in real life.


Like the famous chunk of FAQ that explains this :)

A: No.
Q: Should I post my reply above the quoted text?

Related to that (sorry if I'm impolite, but I have to say it): last time I checked the Usenet convention was
also to *edit your followups*. Please don't add 14 lines of content to 75 lines of redundant, badly-wrapped,
ugly-to-read quotations that are in no direct relation to what you're saying.

Didn't mean to insult you--and I don't claim I'm a guru of Usenet etiquette, either ... but let's keep trying.

Best regards
Hendrik Krauss

Jul 20 '05 #6
>
Try:-
document.getElementById(mylink);
- instead. However, this function does not do anything with the result
of the function call so there is probably no point in calling the
function at all.


This works. I had a string that I fed like:

mylink='my_link_id';
var tableLeft = getOffsetLeft( document.getElementById(mylink) );

function getOffsetLeft(el)
{
var ol = el.offsetLeft;
while ( ( el = el.offsetParent ) != null )
{
ol += el.offsetLeft;
}
return ol;
}

I got the proper value of the offset. Thanks for everyones help.
Jul 20 '05 #7
JRS: In article <3f***********************@news.freenet.de>, seen in
news:comp.lang.javascript, Hendrik Krauss <us****@removethis.hendrik-
krauss.andthat.de> posted at Sat, 4 Oct 2003 14:37:03 :-

Related to that (sorry if I'm impolite, but I have to say it): last time I
checked the Usenet convention was
also to *edit your followups*. Please don't add 14 lines of content to 75 lines
of redundant, badly-wrapped,
ugly-to-read quotations that are in no direct relation to what you're saying.


A further convention that you yourself have ignored is to set the right
margin at (about) 72 characters, at least for text in paragraphs. This
is, in part, for display in traditional-sized windows; but it is also
helpful to the human eye - see any book or magazine.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Christopher Benson-Manica | last post: by
15 posts views Thread by Gabor Drasny | last post: by
2 posts views Thread by cnickl | last post: by
5 posts views Thread by rengeek33 | last post: by
10 posts views Thread by lovecreatesbea... | last post: by
3 posts views Thread by silverburgh.meryl | last post: by
1 post views Thread by OccasionalFlyer | last post: by
8 posts views Thread by David Lazos | last post: by

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.