32

I have a jquery function and I need to call it after opening the website in an Iframe.

I am trying to open a weblink in an Iframe and after opening it I need to call the below function. So how do I do that?

Here is my function:

<script type="text/javascript">
       $(document).ready(function(){
           $("#<%=Button1.ClientID%>").click(function (event) {

            $('#<%=TextBox1.ClientID%>').change(function () {
                $('#various3').attr('href', $(this).val());
            });
            $("#<%=Button2.ClientID%>").click();
        });
      })
    function showStickySuccessToast() {
        $().toastmessage('showToast', {
            text: 'Finished Processing!',
            sticky: false,
            position: 'middle-center',
            type: 'success',
            closeText: '',
            close: function () {

            }
        });
    }

    </script>

This is my button to open the link in an IFrame:

<a id="various3" href="#"><asp:Button ID="Button1" 
runat="server" Text="Button" OnClientClick="Button2_Click"/></a>

Actually this is the simple Page I'm having:

enter image description here

And this is the message enter image description here

coder
  • 13,002
  • 31
  • 112
  • 214

4 Answers4

122

You can just use the normal setTimeout method in JavaScript.

ie...

setTimeout( function(){ 
    // Do something after 1 second 
  }  , 1000 );

In your example, you might want to use showStickySuccessToast directly.

Layke
  • 51,422
  • 11
  • 85
  • 111
3

If you could show the actual page, we, possibly, could help you better.

If you want to trigger the button only after the iframe is loaded, you might want to check if it has been loaded or use the iframe.onload:

<iframe .... onload='buttonWhatever(); '></iframe>


<script type="text/javascript">

    function buttonWhatever() {
        $("#<%=Button1.ClientID%>").click(function (event) {
            $('#<%=TextBox1.ClientID%>').change(function () {
                $('#various3').attr('href', $(this).val());
            });
            $("#<%=Button2.ClientID%>").click();
        });

        function showStickySuccessToast() {
            $().toastmessage('showToast', {
                text: 'Finished Processing!',
                sticky: false,
                position: 'middle-center',
                type: 'success',
                closeText: '',
                close: function () { }
            });
        }
    }

</script>
zequinha-bsb
  • 719
  • 4
  • 10
  • I have updated my question you can have a view at that what I mean!And I'm using fancybox Iframe to display the webpage. – coder Nov 21 '11 at 18:31
  • I don't know much about interacting with frames but I'll risk: how about change the click function to $('#iframeID').child('#button').click()? – zequinha-bsb Nov 21 '11 at 18:44
  • Actually I made it working by giving set time out to button instead of function as var timer = setTimeout(function () { $("#<%=Button2.ClientID%>").click(); }, 9000); and working fine.Anyways thanks for your valuable answer I will try with your suggestions. – coder Nov 21 '11 at 18:46
  • You answer is also the best one but I'm not using normal Iframes so this is also a good option who uses thes normal Iframe with Asp.Net. – coder Nov 21 '11 at 18:49
  • +1 as you have tried so much for me and giving me new way to think. – coder Nov 21 '11 at 18:50
  • in the update form i need a default value to be there and choice should be limited. the above function will execute only when we select. Can anyone help me with it – Starwolf Jan 31 '23 at 09:37
0

try This

setTimeout( function(){ 
    // call after 5 second 
  }  , 5000 );
Love Kumar
  • 1,056
  • 9
  • 10
  • Please include an explanation of how its differ from accepted answer and why this solves the problem would really helps to improve the quality of your post, and probably result in more up-votes. – Android Aug 01 '19 at 07:28
  • Also, let's not forget the fact that this is an almost 8-year-old question, is already answered, and has an accepted answer. Plus, this answer is really poor, doesn't explain anything, or contribute anything new (a duplicate), --- and well, read the first sentence, again. – akinuri Aug 01 '19 at 13:58
0

If you want to call something after N of seconds use the below way.

setTimeout( function(){ 
    alert("welcome");
    }  , 2000 );

and you can call a function in this way :

 setTimeout(showMessage, 2000 );
    
    function showMessage(){
      alert("welcome");
    }

If You want to call a function for each N of seconds. use this way :

setInterval( function(){ 
    // execute me each 2 seconds 
  }  , 2000 );
Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53