ASP.NET中的JavaScript事件处理程序 [英] JavaScript Event Handler in ASP.NET
问题描述
我具有以下iframe控件(打算是类似Facebook的按钮):
I have the following iframe control (intended to be a facebook like button):
<iframe id="likeButton"
src="http://www.facebook.com/plugins/like.php?href="
scrolling="no"
frameborder="0"
style="border:none; overflow:hidden;
width:450px; height:80px;"
onprerender="setupLink()"
</iframe>
我在上面定义的javascript函数如下:
I have the javascript function defined above this as follows:
<script type="text/javascript">
function setupLink()
{
var iframe = $("#likeButton");
var newSrc = iframe.attr("src");
newSrc += encodeURIComponent(location.href) + lblKey.Text;
iframe.attr("src", newSrc);
};
</script>
lblKey是ASP.NET页上的标签,它引用页面的特定部分.但是,据我所知,这个函数没有被调用(如果我在开始时放置了alert(),它什么也没有出现).我是javascript的新手,但是浏览网络上的一些文章将表明,这应该更新iframe上的src属性.
lblKey is a label on the ASP.NET page that references the specific section of the page. However, as far as I can determine, this function doesn’t get called (if I put an alert() at the start it brings nothing up). I’m new to javascript, but looking around at some articles on the web would indicate that this should update the src property on the iframe.
我也尝试了以下方法:
<script type="text/javascript" >
$(function() {
var iframe = $("#likeButton");
var newSrc = iframe.attr("src");
newSrc += encodeURIComponent(location.href) + '<%= lblKey.Text %>';
iframe.attr("src", newSrc);
});
</script>
这不起作用,但也不起作用:
Which doesn't work, but neither does:
<script type="text/javascript" >
$(function() {
alert('Hello');
});
</script>
推荐答案
您是否考虑过使用onload事件并将引用传递给iframe?
Have you considered using the onload event and passing a reference to the iframe?
<script type="text/javascript">
function change_source(iframe)
{
if (iframe.src.indexOf('facebook') < 0)
{
iframe.src = 'http://www.facebook.com/plugins/like.php?href=<%= lblKey.Text %>';
}
}
</script>
像这样的HTML ...
HTML something like this...
<iframe id="likeButton"
src="about:blank"
scrolling="no"
frameborder="0"
style="border:none; overflow:hidden;
width:450px; height:80px;"
onload="change_source(this);"
</iframe>
这篇关于ASP.NET中的JavaScript事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!