Abhi
Abhi

Reputation: 441

How do I capture parent page's url (utm parameters) in iframe page,

I am using below code on page load, but it gives me below error

"Object reference not set to an instance of an object."

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (("" + Request.QueryString["utm_source"] == "") && ("" + Request.QueryString["utm_medium"] == "") || ("" + Request.QueryString["utm_source"] == null) && ("" + Request.QueryString["utm_medium"] == null))
            {
                lblSource.Text = "Direct/Referral";
            }
            else
            {
                try
                {
                    if (Request.UrlReferrer.OriginalString.ToString() != null)
                    {
                        string abc = Request.UrlReferrer.OriginalString.ToString();

                        string[] source = abc.Split('?');
                        string a1 = source[1];

                        a1 = a1.Substring(11);

                        string[] spl = a1.Split('&');

                        utm_source = spl[0];
                        string a2 = spl[1];

                        utm_medium = a2.Substring(11);
                    }
                }
                catch (Exception ex)
                {
                    //Response.Write(ex);
                    lblSource.Text = "Direct/Referral";
                }
            }
            //Response.Write(utm_source + "   " + utm_medium);

                lblSource.Text = utm_source + " " + utm_medium;

        }
    }

Upvotes: 0

Views: 2371

Answers (2)

Arindam Nayak
Arindam Nayak

Reputation: 7462

Yes, you are doing it correctly, but you need to put null checks first before using them. Following are some of codes that can be improved.

Use this if(Request.UrlReferrer!=null && !string.IsNullOrEmpty(Request.UrlReferrer.OriginalString)) instead of if (Request.UrlReferrer.OriginalString.ToString() != null) .

Use this Request.QueryString["utm_source"] != null instead of Request.QueryString["utm_source"] == "", because, it will try to covert it to string for comparison and that value is null , it will error as "Object reference not set to an instance of an object.".

To get exact querystring, from iFrame you can do it like this , instead of string manipulations.

protected void Page_Load(object sender, EventArgs e)
{
    Uri parenturl = new Uri(Request.UrlReferrer.OriginalString);
    string qyr = parenturl.Query;
    NameValueCollection col = HttpUtility.ParseQueryString(qyr);
    String kvalue = col["k"];
    String mvalue = col["m"];
}

Assumption: The above code belongs to test1.aspx and i have one more page test2.aspx which has iFrame with src = test1.aspx . I have used http://localhost:52785/test2.aspx?k=1&m=2 url, so parent page has querystrings as k=1, m=2. See below screenshot, what i got.

enter image description here

Upvotes: 3

Daniel Budick
Daniel Budick

Reputation: 1850

You can try the following JavaScript, but only if the iframe and the site around come from the same domain (same origin policy).

var uri = document.getElementById("IdOfIframe").contentWindow.location.href;

Upvotes: 0

Related Questions