开发者

Call/generate dynamic javascript from silverlight

I'm not sure this is supported but wanted to see if any of you had come up with something creative to work around this.

Is there a way to call javascript from silverlight, without having to define any javascript functions on the aspx page/external js?

I'd like to be able to do something like:

HtmlPage.Window.Invoke("(function () { window.lastErrorMessage = 'foo'; })();")

Which, to forgo conversations about style, I'd agree goes against many best practices rules, but my current purpose is brainstorm some quick-and-dirty error reporting (could you even call it reporting?) before we implement the existing databa开发者_如何学运维se-centric error logging in this solution.

Any thoughts? The idea is to generate something discreet that a user wouldn't feel intruded upon or something too technical (like IE's script error dialog), but something our app support could get a little more info from without access to code bases etc.

Thanks, Matthew


The method you are looking for is Eval not Invoke:-

HtmlPage.Window.Eval("(function () { window.lastErrorMessage = 'foo'; })();")


You could add your JavaScript dynamically to the hosting page DOM using HtmlPage.Document and then execute your added methods. Or are you trying not to modify the page at all?

            HtmlElement head = HtmlPage.Document.GetElementsByTagName("head")[0] as HtmlElement;
            HtmlDocument htmlDocument = HtmlPage.Document;
            HtmlElement scriptElement = htmlDocument.CreateElement("script");
            scriptElement.SetAttribute("type", @"text/javascript");
            scriptElement.SetProperty("text", "function testMe(p) { alert(p); }");
            head.AppendChild(scriptElement);
            // Invoke like this
            HtmlPage.Window.Invoke("testMe", "hello");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜