Results 1 to 3 of 3

Thread: objects.methods

  1. #1
    Join Date
    Feb 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question objects.methods

    Hello,

    I might be missing somthing here, but there really is something I cannot figure out.

    After some reading and thinking, I have managed to work out how to modify the properties of an object. Let's say I have a DIV somewhere in a BODY. I can use :

    var d = document.getElementbyId("div_name")

    then I am free to modify properties for div_name :

    d.style.background-color = #00ffff ;

    that work fine

    But I cannot seem to wor out how to apply a method to an object. this is what I am thinking of. I have a DIV in the middle of a BODY.

    If I write :

    document.write("blablabla") ; it will put blablabla wherever my script is being called from in the document flow.

    What I want is write to my DIV :

    var d = document.getElementbyId("div_name") ;

    and then

    d.write("blablabla") ; in order to write directly to the appropriate DIV.

    Should that work or is there something close, or am i totally out of the blue?



    The question behind all this is, I have methods that apply to objects like document for example. Can I not create an object of type document that can take document's methods? What should I do and read to get a better grasp on this?

    kcirtap Langiv

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Kcirtap
    d.style.background-color = #00ffff ;
    That should fail. The string needs to be quoted and the property name should either be in "camel" notation, or use the setProperty method:

    Code:
    d.style.backgroundColor = '#00ffff';
    
    d.style.setProperty('background-color', '#00ffff', '');
    The colour code could also be changed to either '#0ff' or 'aqua'.

    d.write("blablabla") ; [...]

    Should that work or is there something close, or am i totally out of the blue?
    No. NN4 has something like that, but NN4 is (or should be) dead and the technique ended with it.

    To modify the content of a document after it has loaded requires a totally new approach and there are, in fact, two: the document object model (DOM) and the proprietary innerHTML property.

    The DOM allows the creation, insertion, and removable of both elements and text. It also allows groups of elements to be moved from one part of the document to another. Whilst it is very flexible and powerful, it isn't good at adding large amounts of new content - it just gets messy. Arguably, this is because adding large amounts of new content isn't something one should be doing on the Web as it reduces accessibility and introduces a reliance on scripting.

    The second approach, innerHTML, allows a script to write an arbitrary string containing HTML mark-up into an element. It's simpler to use, though it tends to be slower, particularly on Gecko-based browsers.

    Which you use depends on the modifications you're attempting to make. If it's just plain text or a small amount of HTML, I'd use the DOM. If it's more complicated, use innerHTML.

    Using innerHTML is just like using any other string property:

    Code:
    d.innerHTML = 'blah blah blah';
    That would overwrite the content of d with the given text. If you need to insert or append content, just treat the innerHTML property as any other string variable and use the string manipulation methods like substring, indexOf, and the concatenation operators (+ and +=).

    Using the DOM is more complicated, so I'll leave that for now. Ask if you're interested.

    The question behind all this is, I have methods that apply to objects like document for example. Can I not create an object of type document that can take document's methods?
    No. The document object is a host object which is pre-defined. It represents the entire document so you can't simply construct a new instance. Even if you did, there would be no reasonable relationship with a div element (and the object that represents it) so applying document methods would be meaningless.

    Hope that helps,
    Mike

  3. #3
    Join Date
    Feb 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    the innerHTML will do fine for what I am trying to do right now. In the future, I will simply start reading more about DOM and come back later if I need explanations.

    Thanks for all of this

    Patrick

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •