Problem transferring JavaScript variable to Silverlight
I'm currently trying to pass Silverlight a string value from the parent JavaScript. The value is fine with Firefox, but not with IE.
The string
string fileName = "";
Is declared in the public partial class Mainpage: UserControl in the MainPage.cs In the public MainPage() I have the following which tries to assign fileName it's value.
fileLocation = HtmlPage.Window.Invoke("getFileLocationFromParent").ToString();
Within the Silverlight .aspx page is the function
function getFileLocationFromParent() {
var alertMessage = "";
alertMessage = parent.getFileLocation();
alert(alertMessage);
return parent.getFileLocation();
}
The parent function is simple and returns a variable.
function getFileLocation() {
return fileID; }
fileID is declared as var fileID = ""; in the documentReady section of the parent's JS. It's value is declared on a button click function before Silverlight is loaded. Inside the button click event is:
getID();
Which is defined in the parent JS as:
function getID() {
$.ajax({
type: 'POST',
url: '/Page.aspx/getIDstuff ', //returns a string value
contentType: 'applicatio开发者_运维技巧n/json; charset=utf-8',
dataType: 'json',
success: function (returnValue) {
fileID = returnValue.d;
}
});
}
I use the alert for testing. Using chrome and firefox I see the appropriate string, however in IE I only see a blank string.
Does anyone have any suggestions? Thanks!
First thing I would do is use Fiddler to ensure the request to /Page.aspx/getIDStuff is happening and is returning what you would expect.
Also whilst I'm no JQuery expert but it does look to me like the call to $.ajax will complete asynchronously. There is no guarantee that the ajax call will have completed before the Main constructor executes.
There can be considerable variation on how thread scheduling works between different Javascript implementations.
精彩评论