SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    canada
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using document.createElementNS instead of document.write

    I need to eliminate the document.write for this bit of code

    Code:
    copyright=new Date();
    update=copyright.getFullYear();
    
    document.write("Copyright © 2001-"+ update + " " );
    
    var showtext = "Tecknetix"; 
    var mailpart1 = "webmaster"; 
    var mailpart2 = "tecknetix.com"; 
    
    document.write("<a title='email tecknetix' class='hov' href=" + "mail" + "to:" + mailpart1 + "@" + mailpart2 + ">" + showtext + "<\/a><br />");
    The reason is now I'm using <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" /> with xhmtl 1.1

    Since I've changed the MIME type to application/xhtml+xml from text/html the document.write is invalid now.

    Here is a page with a demo on it so you can see how they changed document.write("<link rel=\"stylesheet\" ...") around.

    http://www.xml.com/lpt/a/2003/03/19/dive-into-xml.html

    I think only the Moz1.4 supports application/xhtml+xml

    You can see it on my 4 page site. http://www.tecknetix.com/

    In IE6 you can read the copyright notice but in Mozilla you can't. But in you go to view > page info in Moz - you can see application/xhtml+xml as the type.

    I little long winded but I hope someone can help.

    Cheers

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, since IE doesn't support the namespaced versions of createElement and setAttribute, you'll need to abstract the process. I use this function for just this sort of thing
    Code:
    document.createHTMLElement = function( elemName, attribs )
    {	
    	if ( document.createElementNS )
    	{
    		var elem = document.createElementNS( "http://www.w3.org/1999/xhtml", elemName );
    		var isNamespaced = true;
    	}
    	else
    	{
    		var elem = document.createElement( elemName );
    		var isNamespaced = false;
    	}
    	if ( typeof attribs != 'undefined' )
    	{
    		for ( var i in attribs )
    		{
    			switch ( true )
    			{
    				case ( i == 'text' )  : elem.appendChild( document.createTextNode( attribs[i] ) ); break;
    				case ( i == 'class' ) : elem.className = attribs[i]; break;
    				default : 
    					if ( isNamespaced )
    					{
    						elem.setAttributeNS( "http://www.w3.org/1999/xhtml", i, '' );
    					}
    					else
    					{
    						elem.setAttribute( i, '' );
    					}
    					elem[i] = attribs[i];
    			}
    		}
    	}
    	return elem;	
    }
    The 2nd argument is an object literal specifying all the attribute/value pairs. Example
    Code:
    var a = document.createHTMLElement( "a", { title:'Email Tecknetix', class:'hov', href:"mailto:" + mailpart1 + "@" + mailpart2, text: showtext } );
    There, in one fail swoop, you create the entire element, namespace it if necessary, and propertly set attributes (Opera likes them to be blank first, then a value set)
    [/CODE]
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    canada
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot beetle for taking the time to write that bit of code - it was much appreciated. It was a little more complicated than I thought.

    I knew I was going to be dealing with some issues if I switched over to "application/xhtml+xml" - the big one being that only Mozilla supports it.

    My hosting company doesn't support it, only application/xml, though things looked okay on my end last night.

    Then using Apache's mod_rewrite module to dynamically change the MIME type for all those other browsers that don't support it.

    Watching 4.x trying to download the page. etc .. etx...

    No wonder that those sites who claim xhtml 1.1 have the "text/html" - just too much nonsense with the other way.

    After an all night reading session on xhtml 2.0 - my line of thinking follows those already stated ... that being, xhtml 2 (as it stands) will be a step back in time.

    But if not, I have that code you wrote put away for a rainy day.


    Cheers

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    canada
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    There, in one fail swoop, you create the entire element, namespace it if necessary, and propertly set attributes (Opera likes them to be blank first, then a value set)

    I'm having trouble implementing your code - that may be due to my not understanding it.

    Do I put this in a external .js file and put the link where it needs to be? Or do I need to add something myself?

    I've tried a few differents ways to solve this, but no go. I do keep getting an error and it points to class:'hov' - when I take out that line I don't get an error but I do end up with a blank page.

    Any suggestions on what I'm doing wrong and how I can simply add this to my site.

    Thanks in advance

    Cheers

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya - I forgot. Send it as a string

    'class':'hov'

    That will work.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •