JavaScript Event Handler in ASP.NET

I have the following iframe control (intended as a facebook 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>

I have a javascript function defined above:

<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 is a label on an ASP.NET page that refers to a specific section of the page. However, as far as I can determine, this function is not called (if I put a warning (), then it does not bring anything in the beginning). I am new to javascript, but looking back at some articles on the Internet, I will point out that this should update the src property on the iframe.

EDIT:

I also tried the following:

<script type="text/javascript" >
$(function() {
    var iframe = $("#likeButton");
    var newSrc = iframe.attr("src");
    newSrc += encodeURIComponent(location.href) + '<%= lblKey.Text %>';

    iframe.attr("src", newSrc);
});
</script>

What doesn't work but doesn't:

<script type="text/javascript" >
$(function() {
    alert('Hello');
});
</script>
+3
source share
2 answers

onload 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 - ...

<iframe id="likeButton"
    src="about:blank" 
    scrolling="no" 
    frameborder="0"         
    style="border:none; overflow:hidden; 
    width:450px; height:80px;"
    onload="change_source(this);"
</iframe>
0

asp.net(, prerender) (, javascript)

, jquery, $(), " , , ". , asp.net api then dom, , api, scriptlet

<script type="text/javascript">
  $(function ()
    {
      var iframe = $("#likeButton");
      var newSrc = iframe.attr("src");
      newSrc += encodeURIComponent(location.href) + "<%= lblKey.Text %>";

      iframe.attr("src", newSrc);
    });
</script>
+2

All Articles