Thursday, June 03, 2010

How to find which page a IFRAME embeded in - Asp.net

There are some common senarios where we need to find which page we are in and which domain the parent page belongs to. Specially when we work with banners and advertiesments this may be useful. However, this post demonstrates how to find domain and file name using round trip to client. But for the first request this parameters will not be available but from the second request onwards those parameters will be available to the code behind.

<%@ Page Language="C#"  %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript" language="javascript"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            var url = parent.location.href.replace('http://''');
            if (url.search('.aspx?') != -1) url = url.split("?")[0];
            var location = window.location.href;
            if (location.search('page') == -1 && location.search('domain') == -1) {
                var parts = url.split("/");
                var d = parts[0];
                if (parts.length > 1) {
                    var fileName = parts[parts.length - 1];
                    var p = fileName.split(".")[0];
                    window.location.href = location + "?page=" + p + "&domain=" + d;
                }
                else
                    window.location.href = location + "?domain=" + d;
            }
        });
    </script>
    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string source = this.Page.Request.QueryString.Get("page");
            string domain = this.Page.Request.QueryString.Get("domain");
            if (!string.IsNullOrEmpty(domain))
            {
                ///
                /// Use domain
                ///
            }
            if (!string.IsNullOrEmpty(source))
            {
                if (source.StartsWith("A"))
                {
                    ///
                    /// A Group page
                    /// 
                }
                else if (source.StartsWith("B"))
                {
                    ///
                    /// B Group page
                    ///
                }
            }
        }       
    </script>
</head>
<body>
    <form id="form1" runat="server">
        Test 
    </form>
</body>
</html>
 

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...