Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I write this more efficently for hiding a parent DIV 1

Status
Not open for further replies.

ironhide1975

Programmer
Joined
Feb 25, 2003
Messages
451
Location
US
How can I write this javascript to pass the variables to the javascript instead of having to keep seperate items.
Code:
<script>
function AskParentToHideMe()
{
   if (typeof(parent.hideIFrame)=='function') parent.hideIFrame();
}
</script>
<script>
function hideIFrame()
{
   document.getElementById("links").style.display = "none";
}
</script>
<script>
function hideIFrame2()
{
   document.getElementById("Addresses").style.display = "none";
}
</script>

 
I'm not sure what the purpose of the AskParentToHideMe function is all about, but you could pass the div name in as a parameter to the other functions instead of writing a new function for each div. Oh, and you don't need a seperate <script> tag for each function either:

Code:
<script type="text/javascript">

function hideIFrame(frameName) {
   document.getElementById(frameName).style.display = "none";
}

</script>

//and call the script like this:

<input type="button" value="hide the frame" onclick="[!]hideIFrame('links')[/!]" />

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
the problem is the button to close is on anotehr page located within an iFRAME.

 
the problem is the button to close is on anotehr page located within an iFRAME.

I think is suppose to be correct, please verify.
Code:
<script type="text/javascript">
function AskParentToHideMe(frameName)
{
   if (typeof(parent.hideIFrame)=='function') parent.hideIFrame(frameName);
}

function hideIFrame(frameName)
{
   document.getElementById(frameName).style.display = "none";
}
</script>

 
So, the button in the frame is invoking a function that exists on the parent page?

Why not just put the function in the frame? I guess it really doesn't matter either way. Nevertheless, the method you posted above would work provided that you passed in the id of the div as a parameter for the AskParentToHideMe function when you click the button.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Ok I try this

AskParentToHideMe(Directory);

and I get an error saying Directory is not defined.

 
That's because you don't have a variable named Directory. You're passing in the id of the div - which would be a string. As such, you have to encapsulate it in quotes:
Code:
AskParentToHideMe([!]"[/!]Directory[!]"[/!]);
Anytime you have a string that's not encapsulated in quotes, you're actually trying to reference a variable by that name. Since you didn't have one defined, you got an error. This also would have worked, but would have been completely unnecessary:
Code:
var Directory = "Directory";
AskParentToHideMe(Directory);

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top