9

I am making an image for my webpage through javascript like so:

photoHTMLString = '<li class = "SliderPhoto"><img src =  "' + ImageArray[x].src_small + '" size = "thumb" onclick = "ShowImagePopUP(' + ImageArray[x].src_big + ')" class = "FacebookSliderPhoto"/></li>';

Whenever I try and click a photo go into ShowImagePopUP I get this error:

missing ) after argument list
[Break On This Error] ShowImagePopUp(http://a8.sph...389_84095143389_5917147_2636303_n.jpg)

It doesn't look like I am missing any ')'s so I am lost on the error. Any suggestions?

4 Answers 4

27

You need to wrap the contents of ShowImagePopUP in quotes:

"ShowImagePopUp(\'' + ImageArray[x].src_big + '\')"

Which should render as:

ShowImagePopUp('http://a8.sph...389_84095143389_5917147_2636303_n.jpg')
               ^ note the quote here

Example: http://jsfiddle.net/V23J6/1/

Sign up to request clarification or add additional context in comments.

3 Comments

The quotes didn't show up either for some reason "ShowImagePopUp(\'' + ImageArray[x].src_big + '\')"
Does ShowImagePopUp('http://a8.sph...389_84095143389_5917147_2636303_n.jpg') work (hard-coded into the page)?
Single quotes are allowed inside URLs, encodeURIComponent('\'') === '\''), so even if you know that the image src is a valid URI, you should probably replace single quotes and backslashes with \' or something similar. ImageArray[x].src_big.replace(/['\\]/g, "\\$&") should do it.
2

try

photoHTMLString = '<li class = "SliderPhoto"><img src =  "' 
                + ImageArray[x].src_small 
                + '" size = "thumb" onclick = "ShowImagePopUP(\"' 
                + ImageArray[x].src_big + '\")" class = "FacebookSliderPhoto"/></li>';

should do the trick and solve your problem leaving intact the uglyness of you code

A function like this one should be a bit readable and ready to use...

function slideElement(image){
    var li=document.createElement('li');
    var img=document.createElement('img');
    li.appendChild(img);
    li.setAttribute('class','SliderPhoto');
    img.setAttribute('class','FacebookSliderPhoto');
    img.setAttribute('size', 'thumb');
    img.setAttribute('src', image.src_small);
    img.setAttribute('onclick', function(){showImagePopUP(image.src_big);});

    return li;
}

Comments

0

The value in ImageArray[x].src_big needs to be quoted.

Try to avoid building HTML by mashing strings together. Using a DOM builder gives code that is much easier to debug.

You'd probably be better off writing this so the function computes the large URI based on the small URI rather than having it hard coded.

Comments

0

Here's some general advice, build up the strings into intermediate variables and then assemble it at the end. You can then use the debugger to find out where you're getting your ' or "s unbalanced. When you have it all built you can coalesce it into a single line if you want or leave it with the intermediate variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.