473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Internet Explorer, createElement and draggable boxes

Hi all,

I have a slight problem with IE, when everything works with firefox.

The goal is to _create_ boxes using the createElement method. And then
making it draggable with the mouse. The code example below is still
very temporary, but I could not get IE to drag the created box, when
firefox does it without problem.

Any hints? :)

The drag n' drop is straight from brainjar.com. Since their code works
for hidden/shown divs, I guess it all has to come from IE's lack of
DOM support and problems with it and the used method? Is there any
solution around this problem?

I'd really like to avoid turning boxes on/off (ie, block/hidden or
visible/invisible) because the potential number of them being present
in the future page can be enormous. The advantage of the createElement
method is that it allows for light pages and a lot of client-side
treatment.

Anyway, here's the code :)

<html>

<head>
<script type="text/javascript" language="javascript">
function Browser() {
var ua, s, i;

this.isIE = false;
this.isNS = false;
this.version = null;

ua = navigator.userAgent;

s = "MSIE";
if ((i = ua.indexOf(s)) >= 0) {
this.isIE = true;
this.version = parseFloat(ua.substr(i + s.length));
return;
}

s = "Netscape6/";
if ((i = ua.indexOf(s)) >= 0) {
this.isNS = true;
this.version = parseFloat(ua.substr(i + s.length));
return;
}

// Treat any other "Gecko" browser as NS 6.1.

s = "Gecko";
if ((i = ua.indexOf(s)) >= 0) {
this.isNS = true;
this.version = 6.1;
return;
}
}
var browser = new Browser();
function activatebox(id,posx,posy,width,height,title,src) {
var mainid = document.getElementById("main");
var boxid = "box" + id;
var myboxstyle = "left:" + posx + ";top:" + posy + ";width:" + width
+ "px;height:" + height + "px";
var myheaderstyle = "";
var myframestyle = "width:" + width + "px;height:" + height + "px;";

if (document.getElementById(boxid)) {
//z-index max
}
else {
mybox = document.createElement("div");
mybox.setAttribute("id",boxid);
mybox.className = "box";
if (browser.isIE) {
mybox.style.posLeft = posx;
mybox.style.posTop = posy;
mybox.style.height = height;
mybox.style.width = width;
}
else {
mybox.setAttribute("style",myboxstyle);
}
mainid.appendChild(mybox);

boxheader = document.createElement("div");
boxheader.className = "boxheader";
boxheader.setAttribute("id","boxheader" + id);
if (browser.isIE) {
boxheader.style.posLeft = "";
}
else {
boxheader.setAttribute("style",myheaderstyle);
}
boxheader.setAttribute("onmousedown","dragStart(ev ent,'" + boxid +
"')");
headertext = document.createTextNode(title);
boxheader.appendChild(headertext);
mybox.appendChild(boxheader);

boxframe = document.createElement("iframe");
boxframe.className = "boxframe";
boxframe.setAttribute("id","boxframe" + id);
boxframe.setAttribute("src",src);
boxframe.setAttribute("frameborder","0");
boxframe.setAttribute("framespacing","0");
boxframe.setAttribute("marginheight","0");
boxframe.setAttribute("marginwidth","0");
if (browser.isIE) {
boxframe.style.width = width;
boxframe.style.height = height;
}
else {
boxframe.setAttribute("style",myframestyle);
}
mybox.appendChild(boxframe);
}
}
// from www.brainjar.com

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {
var el;
var x, y;

if (id)
dragObj.elNode = document.getElementById(id);
else {
if (browser.isIE)
dragObj.elNode = window.event.srcElement;
if (browser.isNS)
dragObj.elNode = event.target;

// If this is a text node, use its parent element.

if (dragObj.elNode.nodeType == 3)
dragObj.elNode = dragObj.elNode.parentNode;
}

// Get cursor position with respect to the page.

if (browser.isIE) {
x = window.event.clientX + document.documentElement.scrollLeft
+ document.body.scrollLeft;
y = window.event.clientY + document.documentElement.scrollTop
+ document.body.scrollTop;
}
if (browser.isNS) {
x = event.clientX + window.scrollX;
y = event.clientY + window.scrollY;
}

// Save starting positions of cursor and element.

dragObj.cursorStartX = x;
dragObj.cursorStartY = y;
dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 10);
dragObj.elStartTop = parseInt(dragObj.elNode.style.top, 10);

if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
if (isNaN(dragObj.elStartTop)) dragObj.elStartTop = 0;

// Update element's z-index.

dragObj.elNode.style.zIndex = ++dragObj.zIndex;

// Capture mousemove and mouseup events on the page.

if (browser.isIE) {
document.attachEvent("onmousemove", dragGo);
document.attachEvent("onmouseup", dragStop);
window.event.cancelBubble = true;
window.event.returnValue = false;
}
if (browser.isNS) {
document.addEventListener("mousemove", dragGo, true);
document.addEventListener("mouseup", dragStop, true);
event.preventDefault();
}
}

function dragGo(event) {

var x, y;

// Get cursor position with respect to the page.

if (browser.isIE) {
x = window.event.clientX + document.documentElement.scrollLeft
+ document.body.scrollLeft;
y = window.event.clientY + document.documentElement.scrollTop
+ document.body.scrollTop;
}
if (browser.isNS) {
x = event.clientX + window.scrollX;
y = event.clientY + window.scrollY;
}

// Move drag element by the same amount the cursor has moved.

dragObj.elNode.style.left = (dragObj.elStartLeft + x -
dragObj.cursorStartX) + "px";
dragObj.elNode.style.top = (dragObj.elStartTop + y -
dragObj.cursorStartY) + "px";

if (browser.isIE) {
window.event.cancelBubble = true;
window.event.returnValue = false;
}
if (browser.isNS)
event.preventDefault();
}

function dragStop(event) {

// Stop capturing mousemove and mouseup events.

if (browser.isIE) {
document.detachEvent("onmousemove", dragGo);
document.detachEvent("onmouseup", dragStop);
}
if (browser.isNS) {
document.removeEventListener("mousemove", dragGo, true);
document.removeEventListener("mouseup", dragStop, true);
}
}
</script>
<link rel="stylesheet" href="gen.css" type="text/css"
media="all"></link>
</head>

<body>

<a href="#" onclick="javascript:activatebox(2,100,150,160,430, 'title2','myframesrc');">create
box en haut à gauche</a>

<div id="main">
<center>
<table border="0" cellspacing="0" cellpadding="0"
style="width:640px;margin:40px 0 0 0">
<tr>
<td width="100%" align="center">
<img src="centernav.jpg" id="centernav">
</td>
</tr>
</table>
</center>
</div>

</body>

</html>
Jul 23 '05 #1
2 4354
http://walterzorn.com has a mature draggable div/img library.
Perhaps that might give you an idea.

Csaba Gabor from Vienna

"louissan" <lo******@caramail.com> wrote in message news:3b*************************@posting.google.co m...
Hi all,

I have a slight problem with IE, when everything works with firefox.

The goal is to _create_ boxes using the createElement method. And then
making it draggable with the mouse. The code example below is still
very temporary, but I could not get IE to drag the created box, when
firefox does it without problem.

Any hints? :)

The drag n' drop is straight from brainjar.com. Since their code works
for hidden/shown divs, I guess it all has to come from IE's lack of
DOM support and problems with it and the used method? Is there any
solution around this problem?

I'd really like to avoid turning boxes on/off (ie, block/hidden or
visible/invisible) because the potential number of them being present
in the future page can be enormous. The advantage of the createElement
method is that it allows for light pages and a lot of client-side
treatment.

Anyway, here's the code :)

<html>

<head>
<script type="text/javascript" language="javascript">
function Browser() {
var ua, s, i;

this.isIE = false;
this.isNS = false;
this.version = null;

ua = navigator.userAgent;

s = "MSIE";
if ((i = ua.indexOf(s)) >= 0) {
this.isIE = true;
this.version = parseFloat(ua.substr(i + s.length));
return;
}

s = "Netscape6/";
if ((i = ua.indexOf(s)) >= 0) {
this.isNS = true;
this.version = parseFloat(ua.substr(i + s.length));
return;
}

// Treat any other "Gecko" browser as NS 6.1.

s = "Gecko";
if ((i = ua.indexOf(s)) >= 0) {
this.isNS = true;
this.version = 6.1;
return;
}
}
var browser = new Browser();
function activatebox(id,posx,posy,width,height,title,src) {
var mainid = document.getElementById("main");
var boxid = "box" + id;
var myboxstyle = "left:" + posx + ";top:" + posy + ";width:" + width
+ "px;height:" + height + "px";
var myheaderstyle = "";
var myframestyle = "width:" + width + "px;height:" + height + "px;";

if (document.getElementById(boxid)) {
//z-index max
}
else {
mybox = document.createElement("div");
mybox.setAttribute("id",boxid);
mybox.className = "box";
if (browser.isIE) {
mybox.style.posLeft = posx;
mybox.style.posTop = posy;
mybox.style.height = height;
mybox.style.width = width;
}
else {
mybox.setAttribute("style",myboxstyle);
}
mainid.appendChild(mybox);

boxheader = document.createElement("div");
boxheader.className = "boxheader";
boxheader.setAttribute("id","boxheader" + id);
if (browser.isIE) {
boxheader.style.posLeft = "";
}
else {
boxheader.setAttribute("style",myheaderstyle);
}
boxheader.setAttribute("onmousedown","dragStart(ev ent,'" + boxid +
"')");
headertext = document.createTextNode(title);
boxheader.appendChild(headertext);
mybox.appendChild(boxheader);

boxframe = document.createElement("iframe");
boxframe.className = "boxframe";
boxframe.setAttribute("id","boxframe" + id);
boxframe.setAttribute("src",src);
boxframe.setAttribute("frameborder","0");
boxframe.setAttribute("framespacing","0");
boxframe.setAttribute("marginheight","0");
boxframe.setAttribute("marginwidth","0");
if (browser.isIE) {
boxframe.style.width = width;
boxframe.style.height = height;
}
else {
boxframe.setAttribute("style",myframestyle);
}
mybox.appendChild(boxframe);
}
}
// from www.brainjar.com

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {
var el;
var x, y;

if (id)
dragObj.elNode = document.getElementById(id);
else {
if (browser.isIE)
dragObj.elNode = window.event.srcElement;
if (browser.isNS)
dragObj.elNode = event.target;

// If this is a text node, use its parent element.

if (dragObj.elNode.nodeType == 3)
dragObj.elNode = dragObj.elNode.parentNode;
}

// Get cursor position with respect to the page.

if (browser.isIE) {
x = window.event.clientX + document.documentElement.scrollLeft
+ document.body.scrollLeft;
y = window.event.clientY + document.documentElement.scrollTop
+ document.body.scrollTop;
}
if (browser.isNS) {
x = event.clientX + window.scrollX;
y = event.clientY + window.scrollY;
}

// Save starting positions of cursor and element.

dragObj.cursorStartX = x;
dragObj.cursorStartY = y;
dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 10);
dragObj.elStartTop = parseInt(dragObj.elNode.style.top, 10);

if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
if (isNaN(dragObj.elStartTop)) dragObj.elStartTop = 0;

// Update element's z-index.

dragObj.elNode.style.zIndex = ++dragObj.zIndex;

// Capture mousemove and mouseup events on the page.

if (browser.isIE) {
document.attachEvent("onmousemove", dragGo);
document.attachEvent("onmouseup", dragStop);
window.event.cancelBubble = true;
window.event.returnValue = false;
}
if (browser.isNS) {
document.addEventListener("mousemove", dragGo, true);
document.addEventListener("mouseup", dragStop, true);
event.preventDefault();
}
}

function dragGo(event) {

var x, y;

// Get cursor position with respect to the page.

if (browser.isIE) {
x = window.event.clientX + document.documentElement.scrollLeft
+ document.body.scrollLeft;
y = window.event.clientY + document.documentElement.scrollTop
+ document.body.scrollTop;
}
if (browser.isNS) {
x = event.clientX + window.scrollX;
y = event.clientY + window.scrollY;
}

// Move drag element by the same amount the cursor has moved.

dragObj.elNode.style.left = (dragObj.elStartLeft + x -
dragObj.cursorStartX) + "px";
dragObj.elNode.style.top = (dragObj.elStartTop + y -
dragObj.cursorStartY) + "px";

if (browser.isIE) {
window.event.cancelBubble = true;
window.event.returnValue = false;
}
if (browser.isNS)
event.preventDefault();
}

function dragStop(event) {

// Stop capturing mousemove and mouseup events.

if (browser.isIE) {
document.detachEvent("onmousemove", dragGo);
document.detachEvent("onmouseup", dragStop);
}
if (browser.isNS) {
document.removeEventListener("mousemove", dragGo, true);
document.removeEventListener("mouseup", dragStop, true);
}
}
</script>
<link rel="stylesheet" href="gen.css" type="text/css"
media="all"></link>
</head>

<body>

<a href="#" onclick="javascript:activatebox(2,100,150,160,430, 'title2','myframesrc');">create
box en haut à gauche</a>

<div id="main">
<center>
<table border="0" cellspacing="0" cellpadding="0"
style="width:640px;margin:40px 0 0 0">
<tr>
<td width="100%" align="center">
<img src="centernav.jpg" id="centernav">
</td>
</tr>
</table>
</center>
</div>

</body>

</html>

Jul 23 '05 #2


louissan wrote:

boxheader.setAttribute("onmousedown","dragStart(ev ent,'" + boxid +
"')");


That is not going to work across browsers, you are better off scripting
the onmousedown property as an object e.g.
boxheader.onmousedown = function (evt) {
dragStart(evt || window.event, boxid);
};

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

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

Similar topics

3
by: Jason | last post by:
I have a web page that contains textareas. These are dynamically added via asp when the page loads based on database records. The user also has the ability to add more text areas via innerhtml. ...
2
by: The_Original_MB | last post by:
I have a task to create tables dynamically, using the javascript DOM. The current method uses a 1px x 1px IFRAME to loop through some data generation stuff, and then call JS functions in the parent...
6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
3
by: james.dixon | last post by:
Hi I was wondering if anyone else had had this problem before (can't find anything on the web about it). I have three select elements (list boxes - from here on I'll refer to them as 'the...
2
by: Howard Jess | last post by:
CLJ -- I've searched the newsgroup and FAQ for info on insertRow(), but didn't see this reported. It seems that Internet Explorer doesn't respond correctly to either insertRow() or...
1
by: -Lost | last post by:
This is more of a post to inform, unless of course I am missing something fundamental, in which case I would appreciate anyone explaining it. Based on Mr. Michaux's camelizeStyle function I...
1
by: avpkills2002 | last post by:
I seem to be getting this weird problem in Internet explorer. I have written a code for parsing a XML file and displaying the output. The code works perfectly fine with ffx(Firefox).However is not...
2
by: swethak | last post by:
Hi, I am getting the problem the problem with google map in Internet Explorer. This map worked fine in mozilla . When i opened the same map in Internet Explorer i am getting the error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.