Launching CFWINDOW with a Dynamic Location

This post is more than 2 years old.

Over on cf-talk, a user asked if it was possible to use cfwindow without either centering the window or using a hard coded x, y position. Instead they wanted it relative to where the user clicked. This was rather easy to do so I thought I'd share the code.

First, we need to take a link and simply call a JavaScript function. In order to know where we clicked, we will use two event values:

This is the test <a href="" onclick="makeWin(event.pageX,event.pageY);return false;">link</a>.

The makeWin function then will do:

function makeWin(x,y) { ColdFusion.Window.create('mywin','Windows Rules','win.cfm',{x:x+25,y:y+25}); }

And, um, that's it! I thought it would be a bit more complex. The +25 on both x and y will launch the window 25 pixels to the right and down from where you clicked. Adjust accordingly to taste.

The complete template (minus win.cfm) is below:

<cfajaximport tags="cfwindow">

<script> function makeWin(x,y) { ColdFusion.Window.create('mywin','Windows Rules','win.cfm',{x:x+25,y:y+25}); } </script>

<h2>Content to push stuff down</h2>

<h2>More content to push stuff down the page vertically...</h2>

<p> This is the test <a href="" onclick="makeWin(event.pageX,event.pageY);return false;">link</a>. </p>

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA