can you guys plese help me to correct the punctuation marks in the javascript below? Found error when i paste in visual studio. I'm not familiar with javascript! Thanks guy...
function showLocalImage(imgname) {
imgname = imgname.replace(/\\/g,”/”);
imgname = imgname.replace(/\’/g,”\\’”);
content = “<img src=\”" + String(imgname) + “\” border=\”0\” height=\”150\” weight=\”150\”>”;
eval(‘document.getElementById(“imagepreview”).innerHTML=\” + content +”‘”);
document.getElementById.imagepreview.style.visibility =’visib开发者_开发问答le’;
}
This is because some of your quote characters are actually from a different character encoding, Use the following code:
function showLocalImage(imgname) {
imgname = imgname.replace(/\\/g,"/");
imgname = imgname.replace(/\’/g,"\\'");
content = "<img src=\"" + imgname + "\" border=\"0\" height=\"150\" weight=\"150\">";
var image_preview = document.getElementById("imagepreview");
image_preview.innerHTML = content;
image_preview.style.visibility = 'visible';
}
A couple of things:
You should reuse DOM result sets and elements as much as possible to avoid having to run a query against the DOM every time you need to reference an element or set of elements.
You should really try to avoid using the
eval
function as much as possible.eval
== EVIL
You seem to use wrong quote characters (” or ‘
). Replace them with " or ' and you'll be good. Use an ascii text editor, not a word processor for typing.
You should avoid eval
, and declare the content
variable, as well as declare a reference to the imagepreview
element:
function showLocalImage(imgname) {
imgname = imgname.replace(/\\/g,"/");
imgname = imgname.replace(/\'/g,"\\'"); //Is this really necessary?
var content = "<img src=\"" + imgname + "\" border=\"0\" height=\"150\" weight=\"150\">";
var img = document.getElementById("imagepreview");
img.innerHTML = content;
img.style.visibility ='visible';
}
精彩评论