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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make Window Unable to Move

Status
Not open for further replies.

SarahKate31

Programmer
Feb 14, 2002
34
US
Hi everyone -- forgive me if i'm duplicating someone elses post...Anyway, I am opening a popup window with window.open and I would like to have the user not be able to move the window around with the mouse. I'd like it to stay anchored to its location...Does anyone know how to do this?
Thanks so much -- Kate
 

The best I can think of would be to use something like setInterval() to check the top and left of the popup window... if it doesn't match your ideal location... then use window.moveTo() to set it back again.

I know that this isn't perfect... but it will provide a solution that works on all browsers (where the user has javascript enabled).

Cheers,
Jeff

 
Jeff - thanks for the idea...
but i cant seem to get it to work...i set the interval like this: (it is being set onload)

var pos = setInterval(CheckLocation(),2000)

function CheckLocation() {
if(window.left!=0) window.moveTo(0,0)
}

it seems to be running only once...and the window is still movable...do i have the syntax wrong?

thanks -- kate
 
This was a lot tricker than I thought it would be. Jeff's idea was a great starting point, however.

In IE, the top and left settings are not the same as those you need to use in the moveTo command - you need to find the height of the toolbars. I found a good way to do this here:


Some things to note:

1. If you are using this from within a frameset, you will need to amend "window." to "window.top.".

2. While it is cross-browser (tested on IE and FF), FF has security restricitons on moving windows off of the screen. This will cause the new location to be different from the original if [any part of] the window was originally off of the screen.

3. The window position is checked every 500 milliseconds (half a second). The smaller you make this, the higher the CPU utilisation will be... so it becomes a balancing act.

4. The try / catch block is needed for IE. If you try to position the window while it is being dragged, very odd security errors appear. This stops those from appearing.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] lang="en" xml:lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta http-equiv="content-language" content="en" />
	<title>Window tracker</title>

	<script type="text/javascript">
	<!--

		// If IE, calculate height of toolbars, etc
		// Found at [URL unfurl="true"]http://webfx.eae.net/dhtml/wincontrols/wincontrols.html[/URL]
		if (window.screenLeft) {
			var oldScreenLeft = window.screenLeft;
			var oldScreenTop = window.screenTop;
			window.moveTo(oldScreenLeft, oldScreenTop);
			var deltaX = window.screenLeft - oldScreenLeft;
			var deltaY = window.screenTop - oldScreenTop;
			window.moveTo(oldScreenLeft - deltaX, oldScreenTop - deltaY);
			var startX = window.screenLeft - deltaX;
			var startY = window.screenTop - deltaY;
		} else {
			var startX = window.screenX;
			var startY = window.screenY;
		}

		function checkPosition() {
			var currentX = (window.screenLeft) ? (window.screenLeft + deltaX): window.screenX;
			var currentY = (window.screenTop) ? (window.screenTop + deltaY) : window.screenY;
			if (currentX != startX || currentY != startY) {
				try {
					window.moveTo(startX, startY);
				}
				catch(e) { }
			}
		}

		setInterval('checkPosition();', 500);
	//-->
	</script>
</head>

<body>
</body>
</html>

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]
 
kate,

change
var pos = setInterval(CheckLocation(),2000)

to
var pos = setInterval("CheckLocation()",2000)

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 

understand that the examples they offer are limited to moving the window back to its original location if, upon the script running after a certain amount of time, the window has been moved. neither will prevent the actual moving of the window...

does the solution have to be cross-browser compatible? is it for in house, or is it for the web?

- g
 
thanks everyone for your help -- i am going to see what i can put together with all of your advice

spewn - in answer to your question, no it does not need to be cross-browser compatible...i only need it to work with IE

thanks again -- kate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top