SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IE7 Script problem - undefined is null or not an object

    Hi, I'm having a problem which is only happening in IE7, while Firefox is working fine. I'm using an AJAX in place editor script to allow users to make instant editing changes to page content. When a user clicks on some text, a textfield is dynamically created where they can make a change to the selected text. To make this work correctly, I had to use a javascript function to convert quotes etc to html entities, otherwise the text in the textfield would be cut off at a double quote. However, since I started using this function, the textfield is no longer created in IE and I get the following error:

    'undefined is null or not an object'

    The html entities function which is causing the problem is as follows:
    Code:
    function htmlentities(str) {
        var i, output = '', len, chr = '';
        len = str.length;
        for(i=0;i<len;i++){
            char = str[i].charCodeAt(0);
            if( (char>47 && char<58)||(char>62 && char<127) ){
                output += str[i];
            }else{
                output += "&#" + str[i].charCodeAt(0) + ";";
            }
        }
        return output;
    }
    then the textfield is created with the following line..
    Code:
    actual.innerHTML = "<input id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" maxlength=\"254\" type=\"text\" value=\"" +  htmlentities(actual.innerHTML) + "\" onkeypress=\"return fieldEnter(this,event,'" + actual.id + "')\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\" />";
    Any ideas what might be causing the problem?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Typo on line 2:
    Code javascript:
    chr = ''
    should be
    Code javascript:
    char = ''

    Although I don't use JS much, so it might or might not solve your problem.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't get the (i-1)th character in a string by using
    str[i] - instead use str.charAt(i)

    and instead of str[i].charCodeAt(0) just use str.charCodeAt(i)

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for the response guys. The function is working correctly now in the format below:

    Code:
    function htmlentities(str) {
        var i =0, output = '', len=0, char1 = '';
        len = str.length;
        for(i=0;i<len;i++){
            char1 = str.charCodeAt(i);
            if( (char1>47 && char1<58)||(char1>62 && char1<127) ){
                output += str.charAt(i);
            }else{
                output += "&#" + str.charCodeAt(i) + ";";
            }
        }
        return output;
    }


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
  •