Very Simple JavaScript Question

  • Thread starter Roy Schestowitz
  • Start date
R

Roy Schestowitz

Let us say I have a JavaScript function called get_one() which returns a
one. I am not entirely sure if the returned value can be accessed only by
another function. Is there a way of doing something like the following?

<font size=get_one()>Hello World</font>

Must I use document.write?

Thanks and sorry about the low level of this question,
Roy
 
H

Hywel Jenkins

Let us say I have a JavaScript function called get_one() which returns a
one. I am not entirely sure if the returned value can be accessed only by
another function.

The value returned can be accessed by any other function or operation as
long as it's within scope.

Is there a way of doing something like the following?

<font size=get_one()>Hello World</font>

Must I use document.write?

Yes, though which version of HTML are you using? Presumably it's an old
one, seeing as you've got a <font> element.
 
R

Roy Schestowitz

Hywel said:
The value returned can be accessed by any other function or operation as
long as it's within scope.


That's what I imagined. I need to fetch a URL for an image. Does it mean I
have to write a block, assign it an ID and then overwrite it with
document.write? I have done this with <div> before but what if I have a
...though which version of HTML are you using? Presumably it's an old
one, seeing as you've got a <font> element.


I simplified a much larger problem. I don't use <font>

Thanks,
Roy
 
R

Roy Schestowitz

Hywel said:
The value returned can be accessed by any other function or operation as
long as it's within scope.



Yes, though which version of HTML are you using? Presumably it's an old
one, seeing as you've got a <font> element.

My problem is now a little different. If I use document.write to write HTML
that has JavaScript within it, the JavaScript does not work. Is there a way
around this?
 
T

Toby Inkster

Roy said:
Let us say I have a JavaScript function called get_one() which returns a
one. I am not entirely sure if the returned value can be accessed only by
another function. Is there a way of doing something like the following?

<font size=get_one()>Hello World</font>

<body onload="document.getElementById('foo').size=get_one();">
<font id="foo">Hello World</font>
</body>

??
 
S

SpaceGirl

Toby said:
Roy Schestowitz wrote:




<body onload="document.getElementById('foo').size=get_one();">
<font id="foo">Hello World</font>
</body>

??

Maybe...:

<script type="text/javascript>
function get_one() {
x = 1;
return x;
}
</script>

<body onload="document.getElementById('foo').style.fontsize=get_one();">
<div id="foo">Hello World</div>
</body>


--


x theSpaceGirl (miranda)

# lead designer @ http://www.dhnewmedia.com #
# remove NO SPAM to email, or use form on website #
 
R

Roy Schestowitz

SpaceGirl said:
Maybe...:

<script type="text/javascript>
function get_one() {
x = 1;
return x;
}
</script>

<body onload="document.getElementById('foo').style.fontsize=get_one();">
<div id="foo">Hello World</div>
</body>

Thanks guys!

I have already done this in PHP (had to learn it as well to get it done)
because I imagined it would be impossible with _simple_ JavaScript.

Took longer, but at least I learned something new...


Roy
 
D

David Håsäther

Roy Schestowitz said:
My problem is now a little different. If I use document.write to
write HTML that has JavaScript within it, the JavaScript does not
work. Is there a way around this?

Maybe, but it sounds like you're trying to solve something in a wrong
way. What are you trying to do in the first place?
 
M

Michael Winter

On Fri, 07 Jan 2005 10:36:23 +0000, SpaceGirl

[snip]
Maybe...:

Getting there... :D
<script type="text/javascript>
^
Forgot the closing quote.
function get_one() {
x = 1;

This would create a global variable, x, when I doubt that's required (nor
desired). Always use the var keyword.
return x;

I'd have thought

return 1;

would be simpler.
[...].style.fontsize=get_one();">

The 's' in fontsize must be capitalised.

Finally, '1' wouldn't be a valid value to be assigned to the fontSize
property; you still have to follow the CSS specification and include units
for length values (or use one of the other accepted value types).

Mike
 
S

SpaceGirl

Michael said:
On Fri, 07 Jan 2005 10:36:23 +0000, SpaceGirl

[snip]
Maybe...:


Getting there... :D
<script type="text/javascript>

^
Forgot the closing quote.
function get_one() {
x = 1;


This would create a global variable, x, when I doubt that's required
(nor desired). Always use the var keyword.
return x;


I'd have thought

return 1;

would be simpler.
}

[...].style.fontsize=get_one();">


The 's' in fontsize must be capitalised.

Finally, '1' wouldn't be a valid value to be assigned to the fontSize
property; you still have to follow the CSS specification and include
units for length values (or use one of the other accepted value types).

Mike

meh. :pppppppppppp

--


x theSpaceGirl (miranda)

# lead designer @ http://www.dhnewmedia.com #
# remove NO SPAM to email, or use form on website #
 
T

Toby Inkster

Michael said:
Finally, '1' wouldn't be a valid value to be assigned to the fontSize
property; you still have to follow the CSS specification and include units
for length values (or use one of the other accepted value types).

Yes -- which is why I used document.getElementById('foo').size instead of
going into the "style" object, as I didn't want to modify the get_one
function.
 
M

Michael Winter

Michael said:
Finally, '1' wouldn't be a valid value to be assigned to the fontSize
property [...]

Yes -- which is why I used document.getElementById('foo').size instead
of going into the "style" object, as I didn't want to modify the get_one
function.

But as the OP revealed in another part of this thread, he isn't using a
FONT element so the size property doesn't apply. :) Though 1 is unlikely
to be the value assigned in the OP's real code, I thought I should just
point out the applicable limitations.

Mike
 
R

Roy Schestowitz

David said:
Maybe, but it sounds like you're trying to solve something in a wrong
way. What are you trying to do in the first place?

It's all done now. I did it in both JavaScript and PHP (separate version for
each). I started with PHP, but I then realised it's too hard to get local
time so I re-implemented this in JavaScript (with your kind help).

Thanks everyone. It's in the very front page below if anyone wants to have a
peek.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top