Javascript Concatenation Question

D

dredge

Hi guys- I need a hand with some Javascript concatenation syntax.
Here's the code:

var current_image_name = this.divImageName
window.alert(document.+current_image_name+.src)

My goal is to have the alert display the url of the document.
[current_image_name].src item (ex: www.mysite.com/images/image1.jpg )
and not the string that points to the item (ex: document.image1.src).
Hopefully this makes sense.

I am just not sure how to concatenate the current_image_name variable
into the window.alert line. The window.alert code above does not work.
 
J

jon

var current_image_name = this.divImageName
window.alert(document.+current_image_name+.src)

My goal is to have the alert display the url of the document.
[current_image_name].src item (ex:www.mysite.com/images/image1.jpg)
and not the string that points to the item (ex: document.image1.src).

All these examples would work. I prefer accessing the window[]
collection myself.

<img id="idTest" name="nameTest" src="http://www.google.com/intl/
en_ALL/images/logo.gif">

<script>

alert(window['idTest'].src);
alert(window['nameTest'].src);
alert(document.nameTest.src);

var name = 'nameTest';
alert(window[name]);
alert(eval('document.' + name + '.src'));

</script>
 
L

Lee

dredge said:
Hi guys- I need a hand with some Javascript concatenation syntax.
Here's the code:

var current_image_name = this.divImageName
window.alert(document.+current_image_name+.src)

My goal is to have the alert display the url of the document.
[current_image_name].src item (ex: www.mysite.com/images/image1.jpg )
and not the string that points to the item (ex: document.image1.src).
Hopefully this makes sense.

I am just not sure how to concatenate the current_image_name variable
into the window.alert line. The window.alert code above does not work.

String concatenation works with string literals (ie, text in quotes)
and string variables.

In your alert line, document and src are Object references, not strings.

Instead of concatenating, you should be using the "square bracket"
notation to access the named image from the images collection:

var current_image_name = this.divImageName
alert(document.images[current_image_name].src)


--
 
T

Thomas 'PointedEars' Lahn

dredge said:
var current_image_name = this.divImageName
window.alert(document.+current_image_name+.src)

My goal is to have the alert display the url of the document.
[current_image_name].src item (ex: www.mysite.com/images/image1.jpg )
and not the string that points to the item (ex: document.image1.src).
window.alert(document.images[current_image_name].src);

Hopefully this makes sense.

It does, to a certain extent.
I am just not sure how to concatenate the current_image_name variable
into the window.alert line.

You don't.
The window.alert code above does not work.

Obviously, as it is syntactically invalid.


PointedEars
 
T

Thomas 'PointedEars' Lahn

jon said:
var current_image_name = this.divImageName
window.alert(document.+current_image_name+.src)

My goal is to have the alert display the url of the document.
[current_image_name].src item (ex:www.mysite.com/images/image1.jpg)
and not the string that points to the item (ex: document.image1.src).

All these examples would work. [nothing that access document.images]

However, neither of those are wise to use as they are proprietary and so not
at all interoperable.

Please learn to quote as recommended by the FAQ:
http://www.jibbering.com/faq/faq_notes/clj_posts.html


PointedEars
 
D

drink.the.koolaid

However, neither of those are wise to use as they are proprietary and so not
at all interoperable.

Please learn to quote as recommended by the FAQ:http://www.jibbering.com/faq/faq_notes/clj_posts.html

PointedEars

As you seem to prefer dispensing criticism rather than wisdom, I'll
give it a go...
Try one of these Dredge:

<html>
<body>
<img id="imgJill" name="Jill" src="http://www.somwhere.com/
pathtojill/jill.jpg">
<img id="imgJack" name="Jack" src="http://www.somwhere.com/
pathtojack/jack.jpg">

<script type="text/javascript">

//The long way (Loop through am array of images and find the right
one.)
function GetImageSrc(imgName){
var src = "";
var arImages = window.document.getElementsByTagName("IMG");
var count = arImages.length;
for(var i=0; i<count; i++){
if(arImages.name==imgName){
src=arImages.src;
break;
}
}
return src;
}

alert(GetImageSrc("Jack"));

//The short way (use an id)
alert(window.document.getElementById("imgJill").src);

</script>
</body>
</html>
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 10/9/2007 2:56 PM:
dredge said:
var current_image_name = this.divImageName
window.alert(document.+current_image_name+.src)

My goal is to have the alert display the url of the document.
[current_image_name].src item (ex: www.mysite.com/images/image1.jpg )
and not the string that points to the item (ex: document.image1.src).
window.alert(document.images[current_image_name].src);

What are your favorite words? I don't recall precisely, but, they are
along the lines of "proprietary and error-prone".

window.alert() is still proprietary and maybe it is even error-prone, but
you are missing the point.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 10/11/2007 3:54 PM:
Randy said:
Thomas 'PointedEars' Lahn said the following on 10/9/2007 2:56 PM:
dredge wrote:
var current_image_name = this.divImageName
window.alert(document.+current_image_name+.src)

My goal is to have the alert display the url of the document.
[current_image_name].src item (ex: www.mysite.com/images/image1.jpg )
and not the string that points to the item (ex: document.image1.src).
window.alert(document.images[current_image_name].src);
What are your favorite words? I don't recall precisely, but, they are
along the lines of "proprietary and error-prone".
window.alert() is still proprietary and maybe it is even error-prone, but
you are missing the point.

I didn't "miss the point" of document.images being the best solution to
the problem.

And I can see nothing wrong, proprietary or error-prone with that.

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268
You missed *my* point.

Which was?


PointedEars
 
T

Thomas 'PointedEars' Lahn

Randy said:
You seem to like to reply to people and tell them how much of the code
they post is "proprietary and error-prone" yet you do the same thing.
Makes me chuckle when I read it. It reads as you being hypocritical.

If you observe more closely, you will *maybe* recognize one day that I
only recommend against proprietary approaches and in favor of a standards
compliant alternative where there I know such an alternative.
Take it in good humor, as it was meant that way.

If only it was not a failed attempt at humor out of ignorance of the context.


PointedEars
 
T

Thomas 'PointedEars' Lahn

drink.the.koolaid said:
However, neither of those are wise to use as they are proprietary and
so not at all interoperable.

Please learn to quote as recommended by the FAQ:
http://www.jibbering.com/faq/faq_notes/clj_posts.html [...]

Which part of that did you not understand?
As you seem to prefer dispensing criticism rather than wisdom,

You have overlooked my other posting (maybe intentionally) which presents
a solution that is more compatible and much more efficient than your own.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 10/12/2007 10:41 AM:

Then you finally admit that there is no "standards compliant
alternative" to window, alert, document and a few other things?

As for "a few other things" I would not go that far. As for the rest, I
have nothing to admit, as I have never stated otherwise. It is just your
crude troll-like interpretation of my postings which attempts to ignore
the context of (my) statements that made you believe I had.

In fact, on several occasions I have said explicitly that a proprietary
property like `document' is necessary as a starting point in the DOM for
implementing standardized interfaces.
You can't even take a joke, sheesh. Lighten up and get a grip Thomas.

Humor is a very subjective thing. And given the general ad-hominem nature
of your pos(t)ings so far, I find it hard to believe you here. It is much
more likely that you are winding around your crude unfounded statements again.


EOD

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top