SitePoint Sponsor

User Tag List

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

    quotes in javascript

    Hi,

    I have a script that uses the contents of a page to display pictures. It works fine in Firefox and Opera, all pictures being seen, but not in IE, I tried all combination of quotes, but none seem to work. The only picture being shown is what is in pit[3] which is hard coded in.

    If I put ' document.write(pit[2]);' in the code, I get the correct output on the page - images/pic19.jpg, but a small x where the picture should be.

    Just not sure how to correct it for IE.

    Part of the script is below
    Code:
    var pit = new Array();
    var w = document.getElementById("pic0").innerHTML;
    var x = document.getElementById("pic1").innerHTML;
    var y = document.getElementById("pic2").innerHTML;
    
        pit[0] = "/images/" + w + ".jpg";
        pit[1] = "/images/" + x + ".jpg";
        pit[2] =  "/images/" + y + ".jpg";
        pit[3] = '/images/pic18.jpg';
    
              images:[pit[0],
                      pit[1],
                      pit[2],
    		  pit[3]],
    Any help appreciated.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've found the culprit, it seems that IE ads a space to the contents of the innerElement. When I do a length count of the variables, w, x or y, I get 1 more in IE than firefox. Try trimming the spaces out by using this:

    String.prototype.trim = function () {
    return this.replace(/^\s*|\s*$/,"");
    }


    y = y.trim();

    but no luck.

    Any ideas?

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The quotes in your array asssignment define the value as a string, but the quotes are not part of the value unless you force them:

    pit[0] = "\"/images/" + w + ".jpg\"";

    To make it plainer, use double quotes where you want an html quoted string,
    and single quotes for script string assignment:

    pit[0] = '\"/images/' + w + '.jpg\"';

    The escapes aren't necessary in the second example,
    but they do no harm and keep your single and double quotes distinct.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,
    Tried that and it did not seem to work, but the space at the end of the value in the innerHtml that IE added was the main problem. Since the value of the innerHtml could be either 4 or 5 characters I did this and it worked.

    Code:
    var pit = new Array();
    var w = (document.getElementById("pic0").innerHTML);
    var x = (document.getElementById("pic1").innerHTML);
    var y = (document.getElementById("pic2").innerHTML);
    
    var w2 = stripped(w);
    var x2 = stripped(x);
    var y2 = stripped(y);
    
    function stripped(fi) {
    	var fie = fi;
    	var fom = fi.substr(4,1);
    	if (isNaN(parseInt(fom))) {
    		fie = fie.substr(0,4);
    	}
    	else {
    		fie = fie.substr(0,5);
    	}
    	return fie
    }
    
        pit[0] = "/images/"+w2+".jpg";
        pit[1] = "/images/" + x2 + ".jpg";
        pit[2] = "/images/" + y2 + ".jpg";


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
  •