472,355 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,355 software developers and data experts.

page event to hide floating div

I've got a floating div which becomes visible when a link is clicked. I
want the div to be hidden when the user clicks anywhere on the page except
for whithin the div. What is the best way to do this? Really, I only need
to support ie6+ but cross browser is always nice.
Jun 27 '08 #1
6 2400
On Jun 3, 11:41 am, "Jeremy" <nos...@please.comwrote:
I've got a floating div which becomes visible when a link is clicked. I
want the div to be hidden when the user clicks anywhere on the page except
for whithin the div. What is the best way to do this? Really, I only need
to support ie6+ but cross browser is always nice.
You could use the BODY element's onClick event handler.

However, this is called whenever you click anything on the page, this
handler will get called.

The big questions are

1) Which will get called first, the floating div or the body?
2) How do you prevent the body's handler being called when you click
on the floating div?

http://www.quirksmode.org/js/events_order.html seems to be a pretty
respectable writeup of this very issue
Jun 27 '08 #2
That's a good idea, the other issue that could happen is that the body
could already have an onclick event, in which case I wouldn't want to
overwrite it, I'd have to write code to handle chaining the events, and
restoring the original event after. As well, because I'm doing this as a
resusable component, what if the onclick event gets overwritten by someone
else.

I stumbled uppon the attachEvent and detachEvent
(addEventListener/removeEventListener for mozilla) methods. They allow you
to keep adding events to an element.
"Dan Rumney" <da*******@warpmail.netwrote in message
news:a0**********************************@c65g2000 hsa.googlegroups.com...
On Jun 3, 11:41 am, "Jeremy" <nos...@please.comwrote:
>I've got a floating div which becomes visible when a link is clicked. I
want the div to be hidden when the user clicks anywhere on the page
except
for whithin the div. What is the best way to do this? Really, I only
need
to support ie6+ but cross browser is always nice.

You could use the BODY element's onClick event handler.

However, this is called whenever you click anything on the page, this
handler will get called.

The big questions are

1) Which will get called first, the floating div or the body?
2) How do you prevent the body's handler being called when you click
on the floating div?

http://www.quirksmode.org/js/events_order.html seems to be a pretty
respectable writeup of this very issue

Jun 27 '08 #3
Lee
This should work with IE6+ and Firefox. I havent tested on any other
browsers.

<html>
<body onclick="HideDiv(event)">
<a href="javascript: ShowDiv()">Show Div</a>
<div id="floating" name="floating" style="border:solid 2px red;width:
200;height:200;display:none;" >WELCOME
<div><input id="test" name="test" value="test"></div>
</div>
<Script>
var myFloatingDiv = "floating"

function ShowDiv() {
document.getElementById("floating").style.display = "block";
}
function HideDiv(e) {
if(!e) e = event;
var myElement = e.target || e.srcElement;
var displyProperty = "none"
for(;myElement != null ;myElement = myElement.parentNode) {
if(myElement.id == myFloatingDiv) {
displyProperty = "block";
break;
}
}
document.getElementById(myFloatingDiv).style.displ ay =
displyProperty;
}
</script>
</body>
</html>
Jun 27 '08 #4
Lee wrote:
This should work with IE6+ and Firefox. I havent tested on any other
browsers.

<html>
<body onclick="HideDiv(event)">
<a href="javascript: ShowDiv()">Show Div</a>
<div id="floating" name="floating" style="border:solid 2px red;width:
200;height:200;display:none;" >WELCOME
<div><input id="test" name="test" value="test"></div>
</div>
<Script>
var myFloatingDiv = "floating"

function ShowDiv() {
document.getElementById("floating").style.display = "block";
}
function HideDiv(e) {
if(!e) e = event;
var myElement = e.target || e.srcElement;
var displyProperty = "none"
for(;myElement != null ;myElement = myElement.parentNode) {
if(myElement.id == myFloatingDiv) {
displyProperty = "block";
break;
}
}
document.getElementById(myFloatingDiv).style.displ ay =
displyProperty;
}
</script>
</body>
</html>
That won't do what the OP is looking for in the case that the user
clicks outside of the floating div, but not directly on the page
background... eg on an image or a button.

Whether this is a significant problem depends on the makeup of the rest
of the page.

That alternative is to prevent the event from bubbling up from the
floating div. That means that the floating div will catch all onClick
events associated with it and its children and prevent them from
reaching the body's onclick handler.
Jun 27 '08 #5
Lee
On Jun 3, 2:37*pm, Dan Rumney <danrum...@77617270mail.netwrote:
Lee wrote:
This should work with IE6+ and Firefox. *I havent tested on any other
browsers.
<html>
<body onclick="HideDiv(event)">
<a href="javascript: ShowDiv()">Show Div</a>
<div id="floating" name="floating" style="border:solid 2px red;width:
200;height:200;display:none;" >WELCOME
<div><input id="test" name="test" value="test"></div>
</div>
<Script>
var myFloatingDiv = "floating"
function ShowDiv() {
* *document.getElementById("floating").style.display = "block";
}
function HideDiv(e) {
* *if(!e) e = event;
* *var myElement = e.target || e.srcElement;
* *var displyProperty = "none"
* *for(;myElement != null ;myElement = myElement.parentNode) {
* * * * * *if(myElement.id == myFloatingDiv) {
* * * * * * * * * *displyProperty = "block";
* * * * * * * * * *break;
* * * * * *}
* *}
* *document.getElementById(myFloatingDiv).style.disp lay =
displyProperty;
}
</script>
</body>
</html>

That won't do what the OP is looking for in the case that the user
clicks outside of the floating div, but not directly on the page
background... eg on an image or a button.

Whether this is a significant problem depends on the makeup of the rest
of the page.

That alternative is to prevent the event from bubbling up from the
floating div. That means that the floating div will catch all onClick
events associated with it and its children and prevent them from
reaching the body's onclick handler.- Hide quoted text -

- Show quoted text -
The request was to hide the div if anything on the page (Image,
button, link, etc.) was clicked other than within the div in
question.

Of course there are instances where this would break. For example: if
a image, button or other element on the page (not in the hidden div)
had script that canceled the bubble.

But, Images and buttons should bubble up to the body onclick event and
fire the HideDiv() function (given that they do not
window.event.cancelBubble = true; as I mentioned earlier). The
HideDiv() function is smart enough to know if the event was fired from
within the div or not.

Of course you could do onclick="window.event.cancelBubble = true;"
inside the hide able div, but you would still need a function that
hides the div on the body. Both my last post and the following code do
the same thing, just in different ways.

Example of cancelBubble in div:
--------------------------------------------------------------------------------------
<html>
<body onclick="HideDiv(event)">
<a href="javascript: ShowDiv()">Show Div</a>
<div id="floating"
name="floating"
style="border:solid 2px red;width: 200;height:200;display:none;"
onclick="window.event.cancelBubble = true;">
WELCOME
<div>
<input id="test" name="test" value="test" onclick="alert('u did
it')">
</div>
</div>
<BR><BR>
<img src="pagerror.gif" onclick="alert('hi')">
<input type=button value=test onclick="window.event.cancelBubble =
true; alert('dont press me')">
<Script>
var myFloatingDiv = "floating"

function ShowDiv() {
document.getElementById(myFloatingDiv).style.displ ay =
"block";
}

function HideDiv(e) {
document.getElementById(myFloatingDiv).style.displ ay = "none"
}
</script>
</body>
</html>
------------------------------------------------------------

Maybe I missunderstood you Dan, have you broken my code :), please
post.
Jun 27 '08 #6
Lee wrote:
[snip]
>That won't do what the OP is looking for in the case that the user
clicks outside of the floating div, but not directly on the page
background... eg on an image or a button.
[snip]
Of course there are instances where this would break. For example: if
a image, button or other element on the page (not in the hidden div)
had script that canceled the bubble.

But, Images and buttons should bubble up to the body onclick event and
fire the HideDiv() function (given that they do not
window.event.cancelBubble = true; as I mentioned earlier). The
HideDiv() function is smart enough to know if the event was fired from
within the div or not.

Doy!

This is just a simple case of Dan being wrong due to late engagement of
brain.

Perhaps I can pretend that this was an attempt at the Socratic Method to
allow you to further explain your code ;o)
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
I have some funky form/event behavior. Access 97. Split frontend/backend, using Access security. I have the same behavior (or lack of behavior) for the pag_Click() event of two separate pages...
4
by: NH | last post by:
Hi, I have a mouseover event on a control, is there a way of opening another asp.net webform and having it close automatically when the mouseoff even occurs? I can easily open a new window...
1
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
10
by: Benton | last post by:
Hi there, I have a UserControl with a couple of textboxes and a couple of buttons ("Save" and "Cancel"). The Click event for this buttons is in the UserControl's codebehind of course, so here's...
2
by: vunet.us | last post by:
Hello JavaScript experts, I have a floating div which I drag all over the page. If the page has scrollbars and users drag the floating div to the very top, page scrolls up too. The problem occurs...
4
by: sid | last post by:
Can someone tell me how to detect "Action Cancelled" page with out polling. I have a frame set and I want to make sure the other frame is displaying what it is supposed to without polling. For...
1
by: mbruyns | last post by:
i have been trying (and sometimes succeeding) to use the modalpopupextender to show various panels of controls on my asp pages. the strange problem that i keep on running into is that sometimes it...
3
by: PhilTheGap | last post by:
Hello, I woulf like to hide an activeX object in a aspx page. The ActiveX is decalred statically in the HTML code, and I'e tried to set the html visible "property" to "false" in the javascript...
1
by: Bali | last post by:
Default.aspx is the starting page containing a control(ascx) which has asp:button control on it. On the button click event it has to open a new page as a modal control. Since refreshing a page in...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.