By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

Need a "static" variable for using window.event.clientX

P: n/a
Hi everyone,

I didn't know how to sum this up very well for the title, but hopefully
the person(s) will come along who can help.

I'm trying to use the window.event.clientX value for positioning a
javascript routine, but I'm having trouble. When the javascript
routine comes up, it comes up in a certain position based on
window.event.clientX. However, the user could click on something in
the script display (say, like a menu, or a calendar, or something),
that my "refresh" (reload that same script but with different values).
Thus, the first time, it works perfectly, but thereafter, it sets the
new x,y values to the most recent window.event.clientX and
window.event.clientY positions. I want it ONLY USE the positions which
were first used.

Does this make sense? Ok, here is the code snippets:

// This is the javascript
function RollOver( Text )
{
HelpText = Text ;
HelpX = window.event.clientX;
HelpY = window.event.clientY;
ToShow = setTimeout( "DoRollOver()", 1, "JAVASCRIPT" );
}

// This is the HTML
<input type="Text" name="timestamp1" value="">
<img src="myImage.gif" onmousedown="RollOver( getText() );">
The first time the image is clicked, the source comes up in the correct
place. But this new script that came up is now the current "event" so
when someone clicks on something to reload the script with a different
value, it sets the position relative to the lastest script placement,
not the original image.

If this doesn't make sense, let me know. Any help is greatly
appreciated!!!

Oct 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Nevermind, I found something that will work. This is what I did:

<script ...>
var HelpX = HelpY = 0 ;

function RollOver( Text )
{
HelpText = Text ;

if( HelpX == 0 )
HelpX = window.event.clientX;

if( HelpY == 0 )
HelpY = window.event.clientY;

ToShow = setTimeout( "DoRollOver()", 1, "JAVASCRIPT" );
}

Thanks, anyway. Hopefully this will help someone else out!

Oct 20 '05 #2

P: n/a
ch***********@gmail.com wrote:
ToShow = setTimeout( "DoRollOver()", 1, "JAVASCRIPT" );


What's the use of the third argument? AFAIK, the setTimeout function
excepts only two...
JW

Oct 20 '05 #3

P: n/a
Janwillem Borleffs wrote:
What's the use of the third argument? AFAIK, the setTimeout function
excepts only two...


Never mind, appears to be an IE thingy...
JW
Oct 20 '05 #4

P: n/a
I'm getting the following error:
getText is not defined
What gives?
Thanks
JM

Oct 21 '05 #5

P: n/a
Zif
ch***********@gmail.com wrote:
Nevermind, I found something that will work. This is what I did:

<script ...>
var HelpX = HelpY = 0 ;

function RollOver( Text )
{
HelpText = Text ;

if( HelpX == 0 )
HelpX = window.event.clientX;
You can simplify the test, and make this cross-browser (see below).

if( HelpY == 0 )
HelpY = window.event.clientY;
If HelpX was not defined, then neither will HelpY be, so if not one, do
both. And use a cross-browser method of finding the location of the event:

if (!HelpX) {
var pos = getPos(e);
HelpX = pos[0];
HelpY = pos[1];

ToShow = setTimeout( "DoRollOver()", 1, "JAVASCRIPT" );
What is the point of using setTimeout with a delay of 1ms? It will
appear instantaneous.
}

Thanks, anyway. Hopefully this will help someone else out!

Only if intended for IE. Cross-browser version (tested in IE an Firefox):
<script type="text/javascript">

var HelpXY;
var onShow;

// Dummy getText function
function getText(){ return 'the Help text'; }

function RollOver(e, txt)
{
var e = e || window.event;
if (!HelpXY {
HelpXY = getPos(e);
}
if (!onShow){
onShow = setTimeout(function(){
showRollOver(HelpXY[0], HelpXY[1], txt);
}, 500);
}
}

function showRollOver(x, y, t)
{
var oDiv = document.createElement('div');
oDiv.style.position = 'absolute';
oDiv.style.left = x + 'px';
oDiv.style.top = y + 'px';
oDiv.style.backgroundColor = '#eeeeee';
document.body.appendChild(oDiv)
oDiv.innerHTML = t;
setTimeout(function() {deleteHelp(oDiv)}, 2000);

}

function deleteHelp(el)
{
el.parentNode.removeChild(el);
onShow = null;
// Comment out next line to have text always appear
// where first click was
HelpXY = null;
}

// Get the mouse position (adapted from quirksmode)
function getPos(e)
{
if (e.pageX || e.pageY){
return [ e.pageX, e.pageY];
} else if (e.clientX || e.clientY) {
return [ e.clientX + document.body.scrollLeft,
e.clientY + document.body.scrollTop];
}
}
</script>

<img src="a.jpg" onmousedown="RollOver(event, getText() );">

--
Zif
Oct 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.