Skip to main content

Microsoft Silverlight

Answered Question Silverlight 3 dynamically loading the silverlight object with javascriptRSS Feed

(0)

apteacher
apteacher

Member

Member

1 points

5 Posts

Silverlight 3 dynamically loading the silverlight object with javascript

Hello, I have been scratching my head about an issue that has perplexed me for a couple of days!  I have a silverlight control that I want to generate dynamically with javascript.  here are the code framents:

function createSL(divid, swidth, sheight, source, initparameters) {

var pluginid = divid + "Plugin";

var divElement = document.getElementById(divid);

var altHTML = divElement.innerHTML;

if (swidth == null)

swidth = '100%';

if (sheight == null)sheight = '750px';

Silverlight.createObjectEx(

{

source: source,

parentElement: divElement,

id: pluginid,

properties:

{

// Plug-in properties

width: swidth,

height: sheight,

minRuntimeVersion: '2.0.31005.0'

},

events:

{

OnError: onSilverlightError // OnError property value -- event-handler function name.

// OnLoad property value -- event-handler function name.

},

 

initParams: initparameters

});

}

 

here is an html frag

 

<body>

<form id="form1" runat="server" >

<div>

<a href="BLOCKED SCRIPTcreateSL('silverlightControlHost', '500px', '400px', 'clientbin/testmediasl3.xap', 'media=bear.wmv')">Bear</a>

<a href="BLOCKED SCRIPTcreateSL('silverlightControlHost', '500px', '400px', 'clientbin/testmediasl3.xap', 'media=appsharing1.wmv')">App Share 1</a>

<button id="myButton" onclick="doIt();">bear</button>

 

 

 

</div>

<div id="silverlightControlHost" >

</div>

 

</form>

<script type="text/javascript">

function doIt() {

var mediaCurrent = document.getElementById("myButton").innerHTML;

mediaCurrent = "media=" + mediaCurrent + ".wmv";createSL('silverlightControlHost', '500px', '400px', 'clientbin/testmediasl3.xap', mediaCurrent);

 

 

//alert(mediaCurrent);
// holds the control open else never opens;

 

 

 

}

 

 

</script>

</body>

 

 

the first two hyperlink controls work just fine and if I had or intended to use static videos would be great, but my intent is to get the video information from the server and will be dynamic so somehow I need to set the initparams of the silverlight object dynamically.  I can us the xsl of a data form web part to set the title of the button and build the initparms from that, the relevant code is in the doIt() function above.  The code doesn't render in IE 8 or FireFox!  However, if the alert is uncommented the control renders in both browsers.  However, I can't have an alert box popping up to show the media! in an actual site!  What am I missing here??  It appears as if the page needs something to happen (the alert box) for the Silverlight control to render.

 

Sledge70
Sledge70

Contributor

Contributor

5882 points

1,042 Posts

Re: Silverlight 3 dynamically loading the silverlight object with javascript

Are you setting the width and height of the actual silverlight plugin you are trying to display?

______________________________________________________
Please mark replies as answers if they answered your question...

Flexman on Silverlight

apteacher
apteacher

Member

Member

1 points

5 Posts

Re: Silverlight 3 dynamically loading the silverlight object with javascript

 I set the hieght and width in the css file: 

#silverlightControlHost{

height:400px;

width:500px;

}

 

I saw another entry about a similar problem: so I also put them in an inline style on the div.

 

In addition in my method call I am setting them to the same height and width.

 

 

Sledge70
Sledge70

Contributor

Contributor

5882 points

1,042 Posts

Re: Re: Silverlight 3 dynamically loading the silverlight object with javascript

I'm talking about within the silverlight control itself.

So for instance if it's showing a usercontrol set it's width in the usercontrol. It might be that your content isn't being displayed properly which results in the internals of your SL plugin being 0 width and height.

______________________________________________________
Please mark replies as answers if they answered your question...

Flexman on Silverlight

apteacher
apteacher

Member

Member

1 points

5 Posts

Re: Re: Silverlight 3 dynamically loading the silverlight object with javascript

Okay! I see the control itself is a builtin expression player out of the Silverlight 3 encoder templates!  Easy to reuse in our own projects.

It has its height and width set to auto. I am assumming as this player can go full screen. However, it is not the control as it will play when it is not created from a javascript onclick  as it will play if called like this:

 

< href="BLOCKED SCRIPT createSL('silverlightControlHost','500px,'400px', 'clientbin/testmediasl3.xap', 'media=bear.wmv')">Bear</a>

it won't play if called from the button click event when called like this:

<button id="myButton" ocnclick="doIt()" >Bear</button>

 

where doIt has the call to createSL

in any event, I changed auto to 800 by 600 on the control and tested it and it still won't display

I need this method as I can dynamically pass the parameter to the doIt function with xsl:  doIt(Bear);

 

apteacher
apteacher

Member

Member

1 points

5 Posts

Re: Re: Silverlight 3 dynamically loading the silverlight object with javascript

In addition, I found that moving the testsite to my server which still has ie7 installed this works in ie7 but not firefox!  If that helps narrow down the problem!

apteacher
apteacher

Member

Member

1 points

5 Posts

Re: Re: Silverlight 3 dynamically loading the silverlight object with javascript

Although, this discussion hasn't answered my original question I was able to get this to work cross browser using the anchor tag

<a> <xsl:attribute name="href">BLOCKED SCRIPTcreateSL('silverlightControlHost2','500px', '400px', '/videos/testmediasl3.xap','media=../videos/<xsl:value-of select="@Title"/>.wmv');</xsl:attribute><xsl:value-of select="@Title"/></a>

So the remaining question seems to be related to this post: http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2009/05/01/silverlight-tip-object-creation-via-silverlight-js-ie8-amp-firefox-tweak.aspx However, it dosen't work in this case!  It is odd that I am calling the same CreateSL method but it works when called from within the page from the anchor tag but not from a function call?? 

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: Re: Silverlight 3 dynamically loading the silverlight object with javascript

Hi Apteacher,

I made a test and it works on my computer.  Below is the test sample.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title> Create Silverlight With JS</title>
    <style type="text/css">
    html, body {
	    height: 100%;
	    overflow: auto;
    }
    body {
	    padding: 0;
	    margin: 0;
    }
    #silverlightControlHost {
	    height:80%;

	    text-align:center;
    }
    </style>
    <script type="text/javascript" src="Silverlight.js"></script>
</head>
<body>
    <form id="form1" runat="server" style="height:100%">
     <div id="silverlightControlHost">
     </div>
    <input type="button" value="Click" onclick="createSL('ClientBin/SLTestGroup.xap')"/>
    <script language="javascript" type="text/javascript">
        function createSL(source) {
            Silverlight.createObjectEx({
            source: source,
            parentElement: document.getElementById("silverlightControlHost"),
                id: "sltest",
                properties: {
                    width: "640",
                    height: "480",
                    background: "white",
                    alt: "<!--not installed-->",
                    version: "3.0.40624.0"
                },
                events: {
                    //onError: onSLError,
                    //onLoad: onSLLoad
                },
                initParams:"param1=1",
                context: "row4"
            });

        }
    </script>
    </form>
</body>
</html>

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: Silverlight 3 dynamically loading the silverlight object with javascript

Hi,

apteacher:
mediaCurrent = "media=" + mediaCurrent + ".wmv";createSL('silverlightControlHost', '500px', '400px', 'clientbin/testmediasl3.xap', mediaCurrent);

Please replace it with

mediaCurrent = "media='" + mediaCurrent + ".wmv'"

Please let me know the result.

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities