javascript function help to get the Element by ID from a Frame's src?
Default.aspx
<html>
<frameset id="MainFrameset" rows="78,*" border="0" framespacing="0" frameborder="0">
<frame application="yes" name="menuFrame" src="<% = GetMenuFrameSrcUrl() %>" scrolling="no" noresize>
</frameset>
</html>开发者_如何学Python
Default.aspx.cs
public string GetMenuFrameSrcUrl()
{
return "http://localhost/Application1/Pages/MenuPage.aspx"
}
MenuPage.aspx
<%@ Register TagPrefix="customControl" TagName="Menu" Src="/Controls/Menu.ascx" %>
<html>
<body bottommargin="0" leftmargin="0" topmargin="0" rightmargin="0">
<form id="Form1" method="post" runat="server">
<customControl:menu id="Menu1" runat="server">
</customControl:menu>
</form>
</body>
</html>
AnotherPage.aspx
<head id="Head1" runat="server">
<script type="text/javascript">
function testFunction(args, name) {
//Need help here...How can I get the reference of Menu1 id?
//I can get this:
alert(top.frames[0].name); //menuFrame
//But don't know how I can take it further to get some Element ID from the MenuPage.aspx which is the source of this Frame???
}
</script>
</head>
Question: How can I get the reference of Menu1 id from testFunction?
Thanks,
Voodoo
The frame document trick is thus:
var frm = top.frames[0];
var doc = frm.contentDocument || frm.Document; // Document (capitalized) in IE, contentDocument otherwise
var menu = doc.getElementById("Menu1");
But, if Menu1 has a different ClientID, you might try this route:
MenuPage.aspx
<script type="text/javascript">
function getMenu() {
return document.getElementByID("<%=Menu1.ClientID%>");
}
</script>
AnotherPage.aspx
var menu = top.frames[0].getMenu();
You can use a snippet of ASP.NET server code to accomplish this. Note this assumes that your javascipt function is contained in the tag of Default.aspx (the same page as the frames).
<head id="Head1" runat="server">
<script type="text/javascript">
function testFunction(args, name) {
var menu1 = document.getElementById('<%=Menu1.ClientID %>');
//note I've never done this with framesets, so it might be
top.frames[0].document.getElementById('<%=Menu1.ClientID %>');
}
</script>
</head>
精彩评论