jquery Plugin: image upload local preview plugin, improve a case of a.

Source: Internet
Author: User
Tags throw exception

/** Name: image upload local preview plugin v1.1* Author: Chou Hsiang * Date: November 26, 2013 * Introduction: Based on jquery extension, image upload preview plugin currently compatible browser (IE Google Firefox) does not support safari* Plugin website:/http keleyi.com/keleyi/phtml/image/16.htm* parameter Description: IMG: Image ID; Width: preview widths; Height: preview altitude; Imgtype: Support file type; Callback: Select the file to display the image after the callback method; * How to use: <div></div><input type = "File" id= "Up"/> The Img tag jacket that needs to be previewed a div then gives the upload control ID The Uploadpreview event $ ("#up"). Uploadpreview ({Img: "IMGPR", width:120 , height:120, Imgtype: ["gif", "JPEG", "JPG", "BMP", "PNG"], Callback:function () {}});*/jQuery.fn.extend ({uploadpreview:function(opts) {var_self = This, _this= $( This); OPTs=jquery.extend ({Img:"IMGPR", Width:100, Height:100, Imgtype: ["GIF", "JPEG", "JPG", "BMP", "PNG"], Callback:function() {}}, opts|| {}); _self.getobjecturl=function(file) {varURL =NULL; if(Window.createobjecturl! =undefined) {URL=window.createobjecturl (file); } Else if(Window. URL! =undefined) {URL=window.            Url.createobjecturl (file); } Else if(Window.webkiturl! =undefined) {URL=Window.webkitURL.createObjectURL (file); }            returnURL;        }; _this.change (function () {            if( This. Value) {                if(! REGEXP ("\. (" + opts. Imgtype.join ("|") + ") $", "I"). Test ( This. Value.tolowercase ())) {Alert ("Select File error, picture type must be" + opts. One of the Imgtype.join (",") + ");  This. Value = ""; if(' headiconimgshow '! = opts. IMG) {//Wuwei Adding 2016-03-30$ ("#" + opts. IMG). attr (' src ', ' ");//Liuhu Adding 20160120                    }                    return false; }                               //Jquery 1.3:1.3 is not recommended for use after: $.browser.msie && $.browser.version, there may be applications in the components you are currently using.                 //JQuery 1.7:1.7                //after JQuery 1.9:1.9, support $.support.msie                //specific browser version to determine the scheme:                //http://zhidao.baidu.com/link?url=LftOa4QYHnDBuOj1HHEnUbmuIgbvd9Ka_yCKWbofx-wDw12l90lIZWn4zyQniwNOE_ 5vqpj-q9cqrzrfoqnl8q                //http://www.jb51.net/article/50463.htm                //http://my.oschina.net/ffwcn/blog/213708                                //above said a lot of, browser version judge to judge to go, here the code, just want to determine whether files are supported by the browser, it is not as good as the Gordian knot, do not throw exception is support, otherwise is not supported.                 Try {                    $("#" + opts. IMG). attr (' src ', _self.getobjecturl ( This. files[0])); } Catch(e) {varsrc = ""; varobj = $ ("#" +opts.                    IMG); vardiv = obj.parent ("div") [0];                    _self.select (); if(Top! =Self )                    {Window.parent.document.body.focus (); } Else{_self.blur (); } src=Document.selection.createRange (). text;                    Document.selection.empty ();                    Obj.hide (); Obj.parent ("Div"). css ({' Filter ': ' Progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=scale) ',                        ' Width ': opts. Width + ' px ',                        ' Height ': opts. Height + ' px '                    }); Div.filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). src =src; } opts.            Callback (); }        })    }});

The original author wrote this plugin, may readily, with the new method of JQuery1.9 $.support.msie, and my reality is that the project has been used JQuery1.7, I dare not rushed to upgrade to 1.9 (not good, can not get the trouble).

I personally infer that the author's idea is to determine whether the files feature of input is supported by judging the type and version of the browser (the author says that Safari is not supported, and of course Apple does not publish the Windows version of Safari and supports a yarn).

If it is IE, is IE10 and above, also support files, is IE9 and the following version, does not support files, call files will naturally throw an exception, by catching the exception, using the Workaroud scheme bypass. Non-IE, think are webkit, are rightfully the standard support files.

The author asked JQuery1.9, we only use JQuery1.7, then had to rewrite $.support.msie, online to determine the best program of IE version, is the regular expression. But the problem is now IE11 after the edge, the agent will not contain the character of MSIE, at least I did not go in with the regular.

Simply thinking out, the Gordian knot, referring to the original author that part of the idea, tube you what version, nothing but to determine whether the browser support files, try not to know, do not throw exceptions to support, or catch the exception on the IE9 under the workaround scheme.

In this case, you Microsoft a fart (hehe, our little bit "2 years old" recently learned to love the curse word) browser, the code is still strong as Taishan.

jquery Plugin: image upload local preview plugin, improve a case of a.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.