IE 5.5: Formatting, URIs, and Stack Operations : The Code | WebReference

IE 5.5: Formatting, URIs, and Stack Operations : The Code


IE 5.5: Formatting, URIs, and Stack Operations

The Code

<HTML>
<HEAD>
<TITLE>A Push Demo</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function bName() {
  if (navigator.appName == "Microsoft Internet Explorer")
    return 1;
  if (navigator.appName == "Netscape")
    return 2;
  return 0;
}
function bVer() {
  // return version number (e.g., 4.03)
  msieIndex = navigator.appVersion.indexOf("MSIE") + 5;
  return(parseFloat(navigator.appVersion.substr(msieIndex,3)));
}
function push() {
  var sub = this.length;
  for (var i = 0; i < push.arguments.length; ++i) {
    this[sub] = push.arguments[i];
	sub++;
  }
}
function pop() {
  var lastElement = this[this.length - 1];
  this.length--;
  return lastElement;
}
function shift(str) {
  var val = this[0];
  for (var i = 1; i < this.length; ++i) {
    this[i-1] = this[i];
  }
  this.length--;
  return val;
}
function unshift() {
  var i = unshift.arguments.length;
  for (var j = this.length - 1; j >= 0; --j) {
    this[j + i] = this[j];
  }
  for (j = 0; j < i; ++j) {
    this[j] = unshift.arguments[j];
  }
}
if (bName() == 1 && bVer() >= 5.5);
else {
  Array.prototype.push = push;
  Array.prototype.pop = pop;
  Array.prototype.shift = shift;
  Array.prototype.unshift = unshift;
}
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
var names = new Array("Tom", "Mark", "Bart", "John");
names.push("Jim", "Richard", "Tim");
alert(names);
var names = new Array("Tom", "Mark", "Bart", "Jonn");
var last = names.pop();
alert("Last = " + last + "  Other = " + names);
var line = new Array("aaa", "bbb", "ccc", "ddd", "eee");
alert("First = " + line.shift() + " Other = " + line);
var line = new Array("ccc", "ddd", "eee");
line.unshift("aaa", "bbb");
alert(line);
</SCRIPT>
</BODY>
</HTML>

Next: A Final Word

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: March 14, 2000
Revised: April 26, 2000

URL: http://www.webreference.com/js/column59/8.html