Important alert: (current site time 7/15/2013 1:03:17 PM EDT)
 

VB icon

Array Manipulators for IE

Email
Submitted on: 8/4/2000 1:39:29 PM
By: Stephane Dorion 
Level: Intermediate
User Rating: Unrated
Compatibility: JavaScript
Views: 12769
(About the author)
 
     Do you likes to do your programming with arrays in javascript? Ever got stuck to do more coding because the pop, shift and unshift functions were not supported by IE? I did, so I built the functions in a cross-browser environment
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Array Manipulators for IE
' Description:Do you likes to do your programming with arrays in javascript? Ever got stuck to do more coding because the pop, shift and unshift functions were not supported by IE? I did, so I built the functions in a cross-browser environment
' By: Stephane Dorion
'
' Inputs:container : The array to be manipulated
'
' Returns:The modified array
'
' Assumes:If you plan to need the value affected by the functions, I suggest you somehow save them first.
If I manage to implement the functions as they are used in Netscape, I will repost a new version here.
'
' Side Effects:Since I don't know how to exactly affect Javascript's DOM I was not able to implement directly these functions as we are used to call them with Netscape (Ex : myArray.pop())
Using my function you will have to do myArray = pop(myArray)
You will lose the value returned by the usual pop function (the last element)
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1910&lngWId=14'for details.'**************************************

//----------------------------------------------------------------------------------------------------
function shift(container)
{
// Does the shift function. I rewrite it since IE does not support it
// Remove 1st element of array and returns the array
// since this is not an implicit method of the array i cannot affect the array directly
 var tmpArray = new Array()
 var i=0
 var maxI = container.length
 
 if (maxI > 0)
 {
for (i=1; i <= maxI-1; i++) //Starting at 1 and 0 zero will skip 1st element, thus removing it.
{
 tmpArray[i-1] = container[i]
}
return tmpArray
 }
 else
return null // There is only one or no element so removing it is like setting array to null
}
function pop(container)
{
// Does the pop function. I rewrite it since IE does not support it
// Remove last element of an array and returns the array
// since this is not an implicit method of the array i cannot affect the array directly
 var tmpArray = new Array()
 var i=0
 var maxI=container.length
 
 if (maxI > 0)
 {
for (i=0; i < maxI; i++) // looping with the < and not the <= will skip the last element, thus removing it.
{
tmpArray[i] = container[i]
}
return tmpArray
 }
 else
return null // There is only one or no element so removing it is like setting array to null
}
function unshift(container)
{
// Function will also receive an unlimited number of parameter (they are the items to add)
// Does the unshift function. I rewrite it since IE does not support it
// Add one or more element to the FRONT of an array and returns the array
// since this is not an implicit method of the array i cannot affect the array directly
 var tmpArray = new Array()
 var i=0
 var maxI = container.length
 for (i=1; i <= arguments.length-1; i++) //Add the items
 {
tmpArray[i-1] = arguments[i]
 }
 for (i=0; i <= maxI-1; i++) // Add the rest of the array
 {
tmpArray[tmpArray.length] = container[i]
 }
 return tmpArray
}
//----------------------------------------------------------------------------------------------------


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.