SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast DizzyDevil's Avatar
    Join Date
    Dec 2004
    Location
    Boston
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question lastChild is null or not an object IE7 only

    I put together this script to insert a space into extra long strings that get returned from a database so the words will wrap. It works in both FF2 and IE7, however IE7 also throws an error: 'lastChild' is null or not an object. Any ideas?

    Code JavaScript:
    var Wordwrap =
    {
    	init: function()
    	{
    		var facet = Core.getElementsByClass("mn_opq");
    		for (var i=0; facet.length; i++)
    		{
    			var howLong = facet[i].lastChild.nodeValue;
    			if (howLong.length > 18)
    			{
    				var splitIt = howLong.split("");
    				splitIt.splice(18,0," ");
    				facet[i].lastChild.nodeValue = splitIt.join("");
    				}
    			}
    		}
     
    };
     
     
     
    Core.start(Wordwrap);


    and the html snippet

    Code HTML4Strict:
    <a class="mn_opq" href="/somewhere">SomeRidiculouslyLongName</a> 
    <a class="mn_opq" href="/somewhere">Normal</a>
    <a class="mn_opq" href="/somewhere">SomeOtherRidiculouslyLongName</a>

    TIA

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The condition on the for loop isn't properly stated.

    Try instead:

    Code javascript:
    for (var i=0; i < facet.length; i++)
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast DizzyDevil's Avatar
    Join Date
    Dec 2004
    Location
    Boston
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! Talk about overlooking the obvious

    Thank you, I really appreciate it.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    That's alright.

    The clue for me was that it works, but it's still throwing up a "lastchild" being null. This points strongly to it falling off the end of the collection and it was an easy check from there of the for loop.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •