Solved

Invalid Argument: document.getElementById(layer).style.left = x + "px";

Posted on 2004-08-30
6
4,849 Views
Last Modified: 2012-05-05
Hello.

The following is giving me an error:

            document.getElementById(layer).style.left = x + "px";

The error is: Invalid Argument

Thanks!

--------------------------
// PHP Layers Menu 3.1.2 (C) 2001-2003 Marco Pratesi (marco at telug dot it)

layerLeft = new Array();
layerTop = new Array();

function setVisibility(layer,on) {
      if (on) {
            if (DOM) {
                  document.getElementById(layer).style.visibility = "visible";
            } else if (NS4) {
                  document.layers[layer].visibility = "show";
            } else {
                  document.all[layer].style.visibility = "visible";
            }
      } else {
            if (DOM) {
                  document.getElementById(layer).style.visibility = "hidden";
            } else if (NS4) {
                  document.layers[layer].visibility = "hide";
            } else {
                  document.all[layer].style.visibility = "hidden";
            }
      }
}

function isVisible(layer) {
      if (DOM) {
            return (document.getElementById(layer).style.visibility == "visible");
      } else if (NS4) {
            return (document.layers[layer].visibility == "show");
      } else {
            return (document.all[layer].style.visibility == "visible");
      }
}

function setLeft(layer,x) {
layerLeft[layer] = x;
      if (DOM && !Opera5) {


////////////////////////// The error is in the one line below ////////////////////

            document.getElementById(layer).style.left = x + "px";
      } else if (Opera5) {
            document.getElementById(layer).style.left = x;
      } else if (NS4) {
            document.layers[layer].left = x;
      } else {
            document.all[layer].style.pixelLeft = x;
      }
}

function getOffsetLeft(layer) {
      var value = 0;
      if (DOM) {      // Mozilla, Konqueror >= 2.2, Opera >= 5, IE
            object = document.getElementById(layer);
            value = object.offsetLeft;
//alert (object.tagName + " --- " + object.offsetLeft);
            while (object.tagName != "BODY" && object.offsetParent) {
                  object = object.offsetParent;
//alert (object.tagName + " --- " + object.offsetLeft);
                  value += object.offsetLeft;
            }
      } else if (NS4) {
            value = document.layers[layer].pageX;
      } else {      // IE4 IS SIMPLY A BASTARD !!!
            if (document.all["IE4" + layer]) {
                  layer = "IE4" + layer;
            }
            object = document.all[layer];
            value = object.offsetLeft;
            while (object.tagName != "BODY") {
                  object = object.offsetParent;
                  value += object.offsetLeft;
            }
      }
      return (value);
}

function setTop(layer,y) {
layerTop[layer] = y;
      if (DOM && !Opera5) {
            document.getElementById(layer).style.top = y + "px";
      } else if (Opera5) {
            document.getElementById(layer).style.top = y;
      } else if (NS4) {
            document.layers[layer].top = y;
      } else {
            document.all[layer].style.pixelTop = y;
      }
}

function getOffsetTop(layer) {
// IE 5.5 and 6.0 behaviour with this function is really strange:
// in some cases, they return a really too large value...
// ... after all, IE is buggy, nothing new
      var value = 0;
      if (DOM) {
            object = document.getElementById(layer);
            value = object.offsetTop;
            while (object.tagName != "BODY" && object.offsetParent) {
                  object = object.offsetParent;
                  value += object.offsetTop;
            }
      } else if (NS4) {
            value = document.layers[layer].pageY;
      } else {      // IE4 IS SIMPLY A BASTARD !!!
            if (document.all["IE4" + layer]) {
                  layer = "IE4" + layer;
            }
            object = document.all[layer];
            value = object.offsetTop;
            while (object.tagName != "BODY") {
                  object = object.offsetParent;
                  value += object.offsetTop;
            }
      }
      return (value);
}

function setWidth(layer,w) {
      if (DOM) {
            document.getElementById(layer).style.width = w;
      } else if (NS4) {
//            document.layers[layer].width = w;
      } else {
            document.all[layer].style.pixelWidth = w;
      }
}

function getOffsetWidth(layer) {
      var value = 0;
      if (DOM && !Opera56) {
            value = document.getElementById(layer).offsetWidth;
      } else if (NS4) {
            value = document.layers[layer].document.width;
      } else if (Opera56) {
            value = document.getElementById(layer).style.pixelWidth;
      } else {      // IE4 IS SIMPLY A BASTARD !!!
            if (document.all["IE4" + layer]) {
                  layer = "IE4" + layer;
            }
            value = document.all[layer].offsetWidth;
      }
      return (value);
}

function setHeight(layer,h) {      // unused, not tested
      if (DOM) {
            document.getElementById(layer).style.height = h;
      } else if (NS4) {
//            document.layers[layer].height = h;
      } else {
            document.all[layer].style.pixelHeight = h;
      }
}

function getOffsetHeight(layer) {
      var value = 0;
      if (DOM && !Opera56) {
            value = document.getElementById(layer).offsetHeight;
      } else if (NS4) {
            value = document.layers[layer].document.height;
      } else if (Opera56) {
            value = document.getElementById(layer).style.pixelHeight;
      } else {      // IE4 IS SIMPLY A BASTARD !!!
            if (document.all["IE4" + layer]) {
                  layer = "IE4" + layer;
            }
            value = document.all[layer].offsetHeight;
      }
      return (value);
}

function getWindowWidth() {
      var value = 0;
      if ((DOM && !IE) || NS4 || Konqueror || Opera) {
            value = top.innerWidth;
//      } else if (NS4) {
//            value = document.width;
      } else {      // IE
            if (document.documentElement && document.documentElement.clientWidth) {
                  value = document.documentElement.clientWidth;
            } else if (document.body) {
                  value = document.body.clientWidth;
            }
      }
      if (isNaN(value)) {
            value = top.innerWidth;
      }
      return (value);
}

function getWindowXOffset() {
      var value = 0;
      if ((DOM && !IE) || NS4 || Konqueror || Opera) {
            value = window.pageXOffset;
      } else {      // IE
            if (document.documentElement && document.documentElement.scrollLeft) {
                  value = document.documentElement.scrollLeft;
            } else if (document.body) {
                  value = document.body.scrollLeft;
            }
      }
      return (value);
}

function getWindowHeight() {
      var value = 0;
      if ((DOM && !IE) || NS4 || Konqueror || Opera) {
            value = top.innerHeight;
      } else {      // IE
            if (document.documentElement && document.documentElement.clientHeight) {
                  value = document.documentElement.clientHeight;
            } else if (document.body) {
                  value = document.body.clientHeight;
            }
      }
      if (isNaN(value)) {
            value = top.innerHeight;
      }
      return (value);
}

function getWindowYOffset() {
      var value = 0;
      if ((DOM && !IE) || NS4 || Konqueror || Opera) {
            value = window.pageYOffset;
      } else {      // IE
            if (document.documentElement && document.documentElement.scrollTop) {
                  value = document.documentElement.scrollTop;
            } else if (document.body) {
                  value = document.body.scrollTop;
            }
      }
      return (value);
}
0
Comment
Question by:hankknight
6 Comments
 
LVL 33

Expert Comment

by:sajuks
ID: 11929867
//Can you post your whole code ?
You have not shown where you are getting the browser version code . Thats why u r getting the error For DOM/Opera...

  this.ver=navigator.appVersion
  this.agent=navigator.userAgent
  this.dom=document.getElementById?1:0
  this.opera5=this.agent.indexOf("Opera 5")>-1
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
  this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  this.ie=this.ie4||this.ie5||this.ie6
  this.mac=this.agent.indexOf("Mac")>-1
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5)
  return this
}

0
 
LVL 33

Expert Comment

by:sajuks
ID: 11929874
//was this what you had missed/left out ?

DOM = (document.getElementById) ? 1 : 0;
NS4 = (document.layers) ? 1 : 0;
Konqueror = (navigator.userAgent.indexOf("Konqueror") > -1) ? 1 : 0;
Konqueror21 = (navigator.userAgent.indexOf("Konqueror 2.1") > -1 || navigator.userAgent.indexOf("Konqueror/2.1") > -1) ? 1 : 0;
Konqueror22 = (navigator.userAgent.indexOf("Konqueror 2.2") > -1 || navigator.userAgent.indexOf("Konqueror/2.2") > -1) ? 1 : 0;
Konqueror2 = Konqueror21 || Konqueror22;
Opera = (navigator.userAgent.indexOf("Opera") > -1) ? 1 : 0;
Opera5 = (navigator.userAgent.indexOf("Opera 5") > -1 || navigator.userAgent.indexOf("Opera/5") > -1) ? 1 : 0;
Opera6 = (navigator.userAgent.indexOf("Opera 6") > -1 || navigator.userAgent.indexOf("Opera/6") > -1) ? 1 : 0;
Opera56 = Opera5 || Opera6;
IE = (document.all) ? 1 : 0;
IE4 = IE && !DOM;
0
 
LVL 4

Expert Comment

by:PeterCN
ID: 11929881
layer should be a string, representing an ID attribute of a htmlElement in your page.
For instance, with:

<div id="test">
   test
</div>

you'd have to call the setLeft function like:

setLeft("test", 123);

Where did you get this script? because it contains code for IE4 and Netscape4, which is basically outdated. All modern browsers suppert the document.getElementById stuff etc.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 16

Author Comment

by:hankknight
ID: 11930050
Thanks.  I'd post the code, but there are five .js libraries, it is a HUGE project.  Maybe it is time to look for something newer . . .

Anyway, it works, but it just gives the error.  

Is there a way to "supress" JS errors, so the errort doesn't show up in the browser?
0
 
LVL 7

Expert Comment

by:searlas
ID: 11930126

Yes,

try { document.getElementById(layer).style.left = x + "px"; } catch (ignore) {}
0
 
LVL 19

Accepted Solution

by:
dakyd earned 500 total points
ID: 11931378
The "invalid argument" bit makes me think that x isn't an integer or a string of integers.  Most likely, x is "undefined" or else it is something like "25px" and you're adding another "px" to the end of it.  The simple way of testing that is to put an alert(x) right before the line that errors out.  Of course, the question then becomes how are you calling setLeft()?  Hope that helps.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now