0

I have the following code for my website:-

function HCreateWindow(top,left,width,height,zindex,parent) {
var handle = Math.floor(Math.random()*10000000000);

if(parent==null) {
document.write("<div id=\"HWINDOW" + handle + "\"style=\"top: " + top + "px; left: " + left + "px;width: " + width + "px;height: " + height + "px; border: 1px solid black;z-index: " +zindex +"\" class=\"drag\"></div>");
} else {
    document.getElementById("HWINDOW" + parent).innerHTML += "<div id=\"HWINDOW" + handle + "\"style=\"top: " + top + "px; left: " + left + "px;width: " + width + "px;height: " + height + "px; border: 1px solid black;z-index: " +zindex +"\" class=\"drag\"></div>";
}
return handle;
}

It turns out that the innerHTML causes internet explorer 8 to say that innerHTML is null or not an object. Thinking that this may be a bug with IE I updated to IE9. Still have the same problem. However, if I add the line before the 'if':-

document.write(parent);

it works, as if innerHTML is recognised. Got no idea how the two commands are related. The only thing is that I do not want to display the contents of 'parent', as this would make the web page untidy. Any clues?

4
  • 3
    Are executing code when the DOM is ready? Commented Oct 29, 2011 at 8:56
  • What is the value of parent? Commented Oct 29, 2011 at 9:22
  • Thanks guys - finding that it was browser incompatibility I resorted to jQuery. The DOM didn't have a chance to load, but there are different methods for different browsers according to window.onload = xxx. It works now. Thanks Commented Oct 30, 2011 at 7:22
  • I doubt it was browser incompatibility. Rather one browser was more lenient than the other for dangerous code Commented Oct 31, 2011 at 18:39

1 Answer 1

1

Assuming you call it inline when you document.write and AFTER the parent exists (poor name for a variable) it should work

function HCreateWindow(top,left,width,height,zindex,parentIdent) {
  var handle = Math.floor(Math.random()*10000000000);
  var html = '<div id="HWINDOW' + handle + '"style="top:' + top + 'px; left:' + left + 'px;width:' + width + 'px;height:' + height + 'px; border:1px solid black;z-index:' +zindex +'" class="drag"></div>';
  if (parentIdent==null) {
    document.write(html); 
  } 
else {
    document.getElementById("HWINDOW" + parentIdent).innerHTML = html;
  }
  return handle;
}

so either

<script>
var hnd = HCreateWindow(10,100,200,400,1000);
</script>

OR

<div id="HWINDOWxxx"></div>

<script>
var hnd = HCreateWindow(10,100,200,400,1000."xxx");
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.