te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Calling A Javascript function from Silverlight - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling A Javascript function from Silverlight - Stack Overflow

programmeradmin3浏览0评论

I'm attempting to call a javascript function (in our code) from a silverlight control. I'm attempting to call the function via:

HtmlPage.Window.Invoke("showPopup", new string[] { "" });

and I get the error "Failed to Invoke: showPopup"

I can call HtmlPage.Window.Invoke("alert", new string[]{"test"}); without issue, but not my own function.

I can also open up the page in question in the IE developer tools and manually call showPopup("") and it works as expected.

So the js function works, and the Silverlight binary can find other js functions. What am I missing here?

Additional Notes:

  • The function call is in a button click event handler, so it happens after the page (and the script) have been loaded)

I'm attempting to call a javascript function (in our code) from a silverlight control. I'm attempting to call the function via:

HtmlPage.Window.Invoke("showPopup", new string[] { "http://www.example." });

and I get the error "Failed to Invoke: showPopup"

I can call HtmlPage.Window.Invoke("alert", new string[]{"test"}); without issue, but not my own function.

I can also open up the page in question in the IE developer tools and manually call showPopup("http://www.example.") and it works as expected.

So the js function works, and the Silverlight binary can find other js functions. What am I missing here?

Additional Notes:

  • The function call is in a button click event handler, so it happens after the page (and the script) have been loaded)
Share Improve this question edited Feb 10, 2009 at 21:12 Ryan asked Feb 10, 2009 at 21:05 RyanRyan 9,9287 gold badges45 silver badges57 bronze badges 1
  • Any idea why I can't do HtmlPage.Window.Invoke("document.getElementById(\"LogoutButton\").click();") ? – John Zabroski Commented Jan 18, 2017 at 16:41
Add a ment  | 

6 Answers 6

Reset to default 2

Aha! I figured it out. Our app uses an iframe, so the rendered html looks something like this

<html>
<head></head>
<body>
Stuff
<iframe>
    <html>
        <head></head>
        <body>Other Stuff</body>
    </html>
</iframe>
<body>
</html>

And the Silverlight control in question is in the iframe. The problem was that the file that contained the showPopup function was referenced in the outer <head> (why I could call the function with the IE toolbar) but not the inner <head>. Adding a reference to the file in the in-the-iframe <head> solved the problem.

Sort of anticlimactic, but thanks for all the help.

Actually referencing the script again from the iframe is not the most efficient way to reference code contained in the parent. If your function is called "showPopup", you can insert this in your iframe:

<script type="text/javascript">
    var showPopup = parent.showPopup;
</script>

And voilà. The explanation for this is that all "global" functions and objects are part of this "global namespace"... which is the "window" object. So if you're trying to access "global" functions from a child, you need to either call the function on the parent (e.g parent.showPopup('....')) or declare a local alias for it (which is what we do in the above example).

Cheers!

Is the showPopup javascript function on the same html or aspx page as the Silverlight control? You will normally get the "Failed to Invoke ..." error if the javascript function does not exist:

HtmlPage.Window.Invoke("functionThatDoesNotExist", new [] { "Testing" });

What browser are you using when you are getting this problem?

Are you using the latest version of Silverlight?

Are you using the ScriptableType attrbiute anywhere?

Is it possible to list the code for a short but plete program that causes this problem to happen on your machine...

Make sure your script is fully loaded before trying to invoke functions from it.

Here's how I do it. But I'm creating silverlight without visual studio. I just have raw html, xaml, and js (javascript). Notice MouseLeftButtonUp and it's value "LandOnSpace"

        <Canvas x:Name="btnLandOnSpace" Background="LightGreen" MouseLeftButtonUp="LandOnSpace"
            Cursor="Hand" Canvas.Top ="0"  Width="70" Height="50"> 
            <TextBlock Text="LandOnSpace"  />
            </Canvas>

function LandOnSpace(sender, e) {  //on server
if (!ShipAnimateActive && !blnWaitingOnServer) {
    blnWaitingOnServer = true;
    RunServerFunction("/sqgame/getJSLLandOnSpace");
        ShowWaitingBox();
        };
else {
    alert('Waiting on server.');
};

}

I had the same problem in VS 2010 with SL 4. I had created a few methods and put them into one single JS file. However this file had not been added to the head section of the ASPX file. Adding it solved the problem. The difference is that though I did not have a separate head section in the iframe, I had the problem and it got solved.

发布评论

评论列表(0)

  1. 暂无评论