1

I have a calendar control that is hosted on an iframe. I get a javascript error when I click on a date on the calendar. window.top.document.getElementById(..) is null or not an object

The iframe is hosted on another page ConfigSettings.aspx

The code in the calendar control code behind is:

Dim sjscript As String = "<script language=""javascript"" type=""text/javascript"">"
sjscript &= "window.top.document.getElementById('" & HttpContext.Current.Request.QueryString("DateTextID") & "').value = '" & Calendar1.SelectedDate & "';"
sjscript &= "window.top.document.getElementById('" & HttpContext.Current.Request.QueryString("DateTextID") & "1').style.display = 'none';"
sjscript = sjscript & "</script" & ">"
Literal1.Text = sjscript  

The html code is:

<input type="text" class="TextBox" id="ToDate" runat="server"/>
    <a href="javascript:ShowCalendar('ToDate1')"><img src="images/Calendar.jpg" border="0" /></a>
                    <iframe src="Calendar.aspx?DateTextID=ToDate" style="display:none; width:200px; height:100px" name="ToDate1" id="ToDate1"></iframe>
<asp:Label runat="server" ID="lblEndTime" Text="End Time:"></asp:Label>

What would be causing the error?

4
  • Is the iframe on the same domain as the top level frame? Commented Sep 11, 2011 at 14:47
  • not sure what you mean "same domain"? It is in the same web application Commented Sep 11, 2011 at 14:50
  • Does the URL of the iframe have the same domain – e.g. http://www.example.com – as the top level window? Commented Sep 11, 2011 at 14:51
  • yes, it does have the same domain Commented Sep 11, 2011 at 14:53

1 Answer 1

1

You're calling "getElementById()" but not checking to see if it's actually found anything. In other words, if no element on the top page has the "id" value you're looking for, the return value from that function will be null. Your code does not check for that eventuality, however.

edit — to check for null, I'd do something like this:

var dateText = window.top.document.getElementById( ... whatever ... );
if (dateText !== null)
  dateText.value = ... whatever ... ;
else
  alert("cannot find date element");

Another possibility is that "window.top" is not really the right window. If there's another layer of <iframe> involved, then "window.top" would skip over the parent of the <iframe> in the code you posted. You might try changing it to "window.parent" instead of "window.top" and see if that helps.

Sign up to request clarification or add additional context in comments.

3 Comments

But all controls exist on the page, so I don't know why it would not find anything. Can you provide a sample of what the code should look like?
Thanks for updating your answer, but it does not tell me what is causing the error.
You EFFING RULE DUDE !!!!!!!!!!!!!!!!!! window.parent worked !! Thanks very much for your help. Your last paragraph hit a home run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.