How do you take the value that a variable is yielding and give it to another variable without giving it the variable information as well in javascript? For instance.
var d = date();
theHour = d.getHours();
document.write(theHour.fontcolor("red"))
I want to change the color of the number put out by theHour but it wont let me because .fontcolor(); isn't a function of date();
In JavaScript, most (if not all?) objects have a toString() method, which converts it into a string. Strings are the type that have the fontcolor() method.
document.write(new Date().getHours().toString().fontcolor('red'));
Yeah, you can't do that because you're using Objects in Javascript.
'Date' only has pure time-related methods/functions.
'theHour' is just a string object. It has no knowledge of style/formatting.
If you wanted to do it the way you want to, you'd have to create a third object, a HTMLSpanElement that DOES have methods to change its style etc.:
[code]var theHour = new Date().getHours();
var theElement = document.createElement('span');
theElement.style.color = 'red'
theElement.style.innerHTML = theHour;
document.body.appendChild(theElement);[/code]
Also I don't think fontcolor() is a function in any object, anyway?
Oh and if you just want the date but in red you could easily do:
[code]document.write('<span style="color:red">'+String(new Date().getHours())+'</span>');[/code]
Where you just write HTML code in your javascript.
EDIT: Fuck, ninja'd by turb, and fuck, there IS a fontcolor() method? And fuck, his answer is waaay simpler :v:
I knew what the problem was just didn't know how to solve it.
Thanks both of you even though I am probably going to use turb's method, tehdoomcat's method is also good to know.
fontcolor('red') just wraps the string in a <font color='red'></font> tag which ever since CSS came about noone really uses any more.
It's far better to use [URL="http://www.facepunch.com/member.php?u=13062"]TehDoomCat's[/URL] method.
Personally I'd just do....
[code]var hour = new Date().getHours(),
element = "<span class='red'>" + hour + "</span>";
document.write( element );[/code]And add a 'red' class to the page's CSS.
[code]
String.prototype.color = function(color) {
return "<span style='color:" + color + ";'>" + this + "</span>";
};
document.write(new Date().getHours().toString().color('red'));
[/code]
[QUOTE=DEADBEEF;23201498]fontcolor('red') just wraps the string in a <font color='red'></font> tag which ever since CSS came about noone really uses any more.
It's far better to use [URL="http://www.facepunch.com/member.php?u=13062"]TehDoomCat's[/URL] method.
Personally I'd just do....
[code]var hour = new Date().getHours(),
element = "<span class='red'>" + hour + "</span>";
document.write( element );[/code]And add a 'red' class to the page's CSS.[/QUOTE]
But font tags are quicker than css.
[QUOTE=jaybu z;23201888]But font tags are quicker than css.[/QUOTE]
Font is deprecated and ugly.
[QUOTE=Wipmuck;23201955]Font is deprecated and ugly.[/QUOTE]
No they're much faster.
[QUOTE=jaybu z;23202380]No they're much faster.[/QUOTE]
How's that?
[QUOTE=jaybu z;23202380]No they're much faster.[/QUOTE]
When downloading the document is magnitudes faster than rendering it, no one gives a [b]fuck[/b].
Providing you use a dynamic often changing page, you can't really cache it. In that case CSS is faster because it would be cached as long as it's in a separate file.
Still that could be worthless also if you put it in with javascript, as javascript is cached (hopefully) in a separate file <font> would be cached with it, providing you don't fetch it with ajax, in which case CSS wins again.
(Also rate me late, but i wanted to say it)
[QUOTE=aualin;23311219]When downloading the document is magnitudes faster than rendering it, no one gives a [b]fuck[/b].[/QUOTE]
Surely you mean slower?
Sorry, you need to Log In to post a reply to this thread.