/    Sign up×
Community /Pin to ProfileBookmark

A pop-up event driver question?

Hi
I’ve been reading the book “JavaScript the definitive guide” to learn more about JavaScript. In this book he gives an example function to create pop-ups. The example function is in the code posted below. This function is shown in the book without examples of how you evoke it. So I added a little more code to try it. When I evoke it was a onclick event to function works fine however when I evoke it with a mouseover event (see commented out code in posting) the firebug debugger generates this error “w has no properties [Break on this error] var d = w.document; // Get its Document object DynamicContent.ht… (line 13)”. So why does evoking this function was an onclick event work while evoking it with an onmouseover event fail?

Sincerely
Marc

[code]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>

<html>
<head>
<title>JavaScript Window Open Example</title>
<link rel=”STYLESHEET” type=”text/css” href=”/style/jsc.css”>
<script language=”javascript” type=”text/javascript”>
<!–
// This function opens a pop-up window. Invoke it from an event handler
// or the pop up will probably be blocked.
function hello() {
var w = window.open(); // Create a new window with no content
var d = w.document; // Get its Document object
d.open(); // Start a new document (optional)
d.write(“<h1>Hello world!</h1>”); // Output document content
d.close(); // End the document
}
window.onload = function(){
document.getElementById(“helloID”).onclick=hello;
//document.getElementById(“helloID”).onmouseover=hello;
};
</script>
</head>

<body>
<a href=”#” id=”helloID”>Link to popup</a>
</body>
</html>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinJul 12.2007 — I would imagine that your function is losing focus and therefore scope.

You should not have it on mouseover anyway as that will REALLY annoy people.
Copy linkTweet thisAlerts:
@MarcMillerauthorJul 13.2007 — I would imagine that your function is losing focus and therefore scope.

You should not have it on mouseover anyway as that will REALLY annoy people.[/QUOTE]

OK

I am just trying to figure out more things about how JavaScript works. The above comment said I was probably losing focus so I tried and onfocus event instead of a onmouseover event with the same result. Which is when I comment out the onclick event and use the onfocus event my code does not work however if I use the onclick event it works perfectly. Can somebody tell me why I cannot keyboard tab my way to starting the pop up?

<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;JavaScript Window Open Example&lt;/title&gt;
&lt;link rel="STYLESHEET" type="text/css" href="/style/jsc.css"&gt;
&lt;script language="javascript" type="text/javascript"&gt;
&lt;!--
// This function opens a pop-up window. Invoke it from an event handler
// or the pop up will probably be blocked.
function hello() {
var w = window.open(); // Create a new window with no content
var d = w.document; // Get its Document object
d.open(); // Start a new document (optional)
d.write("&lt;h1&gt;Hello world!&lt;/h1&gt;"); // Output document content
d.close(); // End the document
}
window.onload = function(){
//document.getElementById("helloID").onclick=hello;
document.getElementById("helloID").onfocus=hello;
};
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;a href="#" id="helloID"&gt;Link to popup&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@CrazyMerlinJul 13.2007 — no, you missed my point.

when I said you were losing focus, I meant your function, not your object.

if onclick works, stick with it.

if the popup ever appears accidently like someone moved the mouse over it by accident, or tabbed to it without realizing, that is a design flaw.

you user should only ever see a popup if they specifically clicked an item that is designed to display a popup and only doing so knowing that.

anything else is bad design!

what is the insistence of using a mouseover?

at the end of the day it is your code, but if you start with bad design, you will end up with annoyed visitors.
×

Success!

Help @MarcMiller spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...