开发者

Using createElement('img') to get .jpeg instead of .png

I would like to use createElement("img') to create .jpeg extension instead of .png

According to Flanagan's book JavaScript: The Definitive Guide: Activate Your Web Pages By David Flanagan,

For jpeg image type, the second argument should be a number between 0 and 1 specifying the image quality level.

I am not sure what the syntax for the code would be.

开发者_Python百科

Is it something like this?

createElement("img",1)


The book is talking about html5's canvas tag and specifically its .toDataURL method.

I think you want to do something like:

var img = document.createElement('img');
img.src = 'myImageSource.jpg';

In this case, it's up to the web server to deliver the image and its type information to the web browser.


The book that you're referring to seems to be talking about the toDataURL method of the canvas API (see this), which accepts type and quality arguments.

You can use document.createElement without worrying about MIME types or quality.


you can use setAttrebure() Method or built-in 'src' property.

var img = document.createElement('img');
img.setAttibute('src', 'img.jpg');
document.body.appendChild(img);

or use

 var img = document.createElement('img');
 img.src = "img.jpg";
 document.body.appendChild(img);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜