Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

jQuery Plugin for Silverlight 3 embedding RSS

1 reply

Last post May 26, 2010 07:51 AM by Jonathan Shen – MSFT

(0)
  • darkthread

    darkthread

    Member

    1 Points

    5 Posts

    jQuery Plugin for Silverlight 3 embedding

    May 20, 2010 11:05 PM | LINK

    I am always looking for a jQuery plugin to embed Silverlight application in web pages, and the most close thing I found is jQuery media plugin, a general purpose plugin for Flash, Quicktime, Windows Media Player, Real Player, MP3, Silverlight, which missing some basic features (ex: default error handling function) for Silverlight.

    So, I just wrote my own plugin for Silverlight 3 application embedding, welcome for feedback.

    jquery silverlight

  • Jonathan Shen – MSFT

    Jonathan She...

    All-Star

    50156 Points

    4951 Posts

    Microsoft

    Re: jQuery Plugin for Silverlight 3 embedding

    May 26, 2010 07:51 AM | LINK

    Hi Darkthread,

    //jQuery Plugin for Silverlight 3 Embedding
    //Ver 0.9 by Jeffrey Lee, http://blog.darkthread.net
    (function ($) {
        if (!window.onSilverlightError) {
            window.onSilverlightError = function (sender, args) {
                var appSource =
                            (sender != null && sender != 0) ?
                            appSource = sender.getHost().Source : "";
     
                var errorType = args.ErrorType;
                var iErrorCode = args.ErrorCode;
     
                if (errorType == "ImageError" || errorType == "MediaError") {
                    return;
                }
                var errMsg = [];
                errMsg.push("Unhandled Error in Silverlight Application " + appSource);
                errMsg.push("Code: " + iErrorCode + "    ");
                errMsg.push("Category: " + errorType + "       ");
                errMsg.push("Message: " + args.ErrorMessage + "     ");
     
                if (errorType == "ParserError") {
                    errMsg.push("File: " + args.xamlFile + "     ");
                    errMsg.push("Line: " + args.lineNumber + "     ");
                    errMsg.push("Position: " + args.charPosition + "     ");
                }
                else if (errorType == "RuntimeError") {
                    if (args.lineNumber != 0) {
                        errMsg.push("Line: " + args.lineNumber + "     ");
                        errMsg.push("Position: " + args.charPosition + "     ");
                    }
                    errMsg.push("MethodName: " + args.methodName + "     ");
                }
                throw new Error(errMsg.join("\n"));
            }
        }
     
        //seed for unique function name
        if (!window.dtsl_FuncUid)
            window.dtsl_FuncUid = 1000;
     
        $.fn.insertSilverlight = function (source, options) {
            var defaults = {
                onError: "onSilverlightError",
                background: "white",
                minRuntimeVersion: "3.0.40818.0",
                autoUpgrade: "true",
                enableHtmlAccess: "true",
                initParams: null,
                splashScreenSource: null,
                onSourceDownloadProgressChanged: null,
                onSourceDownloadComplete: null,
                onLoad: null,
                onResize: null,
                windowless: false,
                width: "100%",
                height: "100%"
            };
            var settings = $.extend(defaults, options);
            return this.each(function () {
                var a = [];
                a.push('<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="' + settings.width + '" height="' + settings.height + '">');
                a.push('<param name="source" value="' + source + '"/>');
                a.push('<param name="onError" value="onSilverlightError" />');
                for (var p in settings) {
                    if (p == "width" || p == "height") continue;
                    var v = settings[p];
                    if (v !== null) {
                        //event callback
                        if (p.indexOf("on") == 0 && $.isFunction(v)) {
                            var funcId = "ftsl_Func" + (window.dtsl_FuncUid++);
                            window[funcId] = v;
                            v = funcId;
                        }
                        a.push('<param name="' + p + '" value="' + v + '" />');
                    }
                }
                a.push('<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=' + settings.minRuntimeVersion + '" style="text-decoration:none">');
                a.push('<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>');
                a.push('</a>');
                a.push('</object>');
                a.push('<iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>');
                $(this).prepend(a.join('\n'));
            });
        }
    })(jQuery);
    Amazing!  Thanks for your sharing.
    Best regards,
    Jonathan
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework