Jump to content

confused!


Drycodez

Recommended Posts

If maybe i want to put a string method (like fontcolor()) in another function, so that it ll change the colour of a string, how can this be done? Why is it that the following dont work?

function abc(a){fontcolor(a)} document.write("string".abc("red"))

Link to comment
Share on other sites

um, string's dont have a method called fontcolor. If you make your own functions in the global scope, you can use them anywhere (or declare them anywhere without var). Or are you asking about how to to prototype the string object to give it a fontcolor method? That won't work since font color is a CSS style attribute and you would need a reference to an element.edit: Are you just trying to make a generic color changing function? If you are, you should make it so it takes two params, the element with the text to be changed, and the color.

function fontColor(ele, color){	ele.style.color = color;};..<p onclick="fontColor(this, 'red')" >I am some text that will change color</p>

Link to comment
Share on other sites

A string object does have a fontcolor method. It's good for almost nothing. The result looks something like this:<span style="color:#f00">my string</span>Which you can do yourself. Be aware that document.write is almost useless. Do yourself a favor and stop practicing with it.

Link to comment
Share on other sites

The method exists, but it's non-standard and causes <font> tags to be written out, which generally shouldn't be used. If you want to change the appearance of text through Javascript, add and remove classes to the element instead. And the reason this doesn't work:document.write("string".abc("red"))Is because your abc function is just a regular function, not a string method. If you want to make it a string method you need to define it on the string prototype:

String.prototype.abc = function(a){  this.fontcolor(a);}

But it's pretty pointless to make a function that's just an alias for another function.

Link to comment
Share on other sites

But it's pretty pointless to make a function that's just an alias for another function.
Unless it's a significantly shorter alias....I see stuff like this all the time:
function get(id) {   return document.getElementById(id);}

Link to comment
Share on other sites

Is document.write useless because you can write all the mark up as is without having to use document.write or is there another reason?In the JS tutorial(w3schools), this is noted about document.write(); :

Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial.
Can anyone elaborate on that?(I know what the last sentence means(about demonstrating) but before that, not sure.)
A string object does have a fontcolor method. It's good for almost nothing. The result looks something like this:<span style="color:#f00">my string</span>Which you can do yourself. Be aware that document.write is almost useless. Do yourself a favor and stop practicing with it.
Link to comment
Share on other sites

document.write has limited(!) utility when documents are first being written. Like, maybe your user's name is saved in a cookie. To personalize the page, you might read the cookie, get the name, and use document.write to stick "Hello, Don!" in a corner somewhere. Long ago, that was the only way to do that. Today, there are better ways.You might want to write dynamic data to a document in a window opened by JavaScript. I don't see it too much, but it happens.What a lot of people new to JS don't realize if that using document.write AFTER the document has completely loaded will totally obliterate the current document. All its content will be replaced by the data passed to document.write. In effect, document.write says, "We're starting a new document. Throw away the old one. Here is the content for the new document." We often see a lot of very confused developers wondering what happened to their pages.If you need to add dynamic content to a document, the preferred technique (currently) is to manipulate some element's innerHTML property. I say "currently" because the W3C has a more standard way to do the job (element.textContent), and every browser supports it, except IE versions less than 9. So for now, innerHTML does the job.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...