Friday 3 February 2012

Crossdomain Calls Using Window Name

ParentPage Js File:-

         //Last recieved value.
         var last = 0;

        //Send message to iframe.
        function toIFrame() {

            //Get the iframe.
            var iFrame = document.getElementById("iframepage").contentWindow;

            //Send the message to iframe.
            iFrame.name = "message=" + new Date().getTime();
        }

    <div>
        <div>
            <input type="button" value="ToIFrame" onclick="return toIFrame();" />
        </div>
        <iframe id="iframepage" src=" iframe page url "
            width="400" height="300" border="1"></iframe>
    </div>


IFramePage:-
        //Last recieved value.
        var last = 0;

        //Get the message on time interval.
        function getMessage() {

            //Get the window name.
            var wName = window.name;

            //Check the window name value.
            if (wName) {

                //Message
                var message;

                //Get the messgage.
                message = wName.split('=')[1];

                //Check the last value with current value.
                if (last != message) {

                    //Assign last value.
                    last = message;

                    //Append the message to body.
                    $('<div/>').html(message).appendTo('#output');
                }
            }
        }

        //Timer for getting messages.
        setInterval(getMessage, 200);


    <div id="output">
    </div>



Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com

No comments:

Post a Comment