开发者

Problem while applying source to <img> tag through javascript

开发者 https://www.devze.com 2023-01-29 05:31 出处:网络
Hi, I have stored images in my database like this images 123.jpg And also in my image folder when i am fetching image name through ajax and applying that image like this

Hi,

I have stored images in my database like this images 123.jpg

And also in my image folder

when i am fetching image name through ajax and applying that image like this

var开发者_运维知识库 result="<img src='folder/images 123.jpg' />"
document.getElementById("div_name").innerHTML=result;

I am getting this in Firebug

<img 123.jpg="" src="folder/images">

i dont want to replace space in image name with any character like "_"

How to make it possible.

Thanks in advance.


Try this:

var result="<img src='folder/images%20123.jpg' />"

%20 is a SPACE (ASCII 32) in URL encoding.


Try this:

var result = "<img src=\"folder/" + encodeURIComponent("images 123.jpg") + "\" />";

The encodeURIComponent will encode the required characters for you.


You need to URL encode the space - either replace it with a + or a %20:

var result="<img src='folder/images%20123.jpg' />"

Or:

var result="<img src='folder/images+123.jpg' />"


You just need to replace the space with a %20 and it should work.

var result="<img src='folder/images%20123.jpg' />"
document.getElementById("div_name").innerHTML=result;


You can use escape(string) to escape the space.

var url = escape('folder/images 123.jpg');
var result='<img src='+ url + '/>'
document.getElementById("div_name").innerHTML=result;
0

精彩评论

暂无评论...
验证码 换一张
取 消