1

I have four product gallery with the possibility to check the detail of each product from each gallery. Every time the user enter in the detail of any product an array is filled with all the products from that gallery. I should tell this is one single page.

I want to completely delete all the records in the array when the user exits the detail view, because when the user enter again in the detail view the array length increments his size.

I already tried arrayName.length =0; and arrayName.length = [] and it deleted the previous data, but his size continue incrementing like this:

1st time detail view is loaded --> arrayName{valA,valB,valC}

2nd time detail view is loaded --> arrayName{,,,val1,val2,val3}

what it's supposed to be in the 2nd time is: arrayName{val1,val2,val3}

Any idea in how I can solve this issue??

Thanks all

The solution

Thanks all guys. It was my problem.

while($rowDetails = mysql_fetch_array($rsDetails)){
?>
        <script language="javascript">
            arrayProd[pos] = <?php echo $rowDetails['RefArticleID']; ?>;
            pos++;
        </script>
   ... 

The array is filled into a while cycle and it increments the pos. At the same time I re-initialize the array I force the pos to be 0 (zero)

arrayProd = [];
pos =0;

Thank you all

3 Answers 3

11

This one is really simple, just re-initialize the array:

arrayName = [];
Sign up to request clarification or add additional context in comments.

Comments

3

Did you try re-initializing the array by setting it to an empty array?

arrayName = [];

1 Comment

That isn't 're-declaring' the array, it is assigning a new array object to the variable arrayName (i.e. initialising, per SoWeLie's answer). Declaring a variable a second time (i.e. var arrayName;) has no effect on its value.
3

I already tried arrayName.length =0;

That should work, except perhaps for browsers that don't comply with ECMA-262 ed 3 — which shoul be very, very few. In which browser does it fail for you?

and arrayName.length = []

Presumably you meant (otherwise you would see an error):

arrayName = [];

which should work without exception.

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.