www.webdeveloper.com
Results 1 to 6 of 6

Thread: Strange Javascript Problem in Opera

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Strange Javascript Problem in Opera

    Hello World,

    I'm having a strange problem with one of my Javascript functions that only takes place in Opera. All other browsers (IE, FF, Chrome, Safari) work perfectly fine with it. I'm basically hiding a couple of div elements when all cookies with the string "compare" have been deleted (cookies are created in another function). Here's the code:

    function removeCookie(item)
    {
    var getdiv1 = document.getElementById("div1");
    var getdiv2 = document.getElementById("div2");

    getdate.setTime(getdate.getTime() + (-1));
    var expires = "; expires="+getdate.toGMTString();
    document.cookie = item + expires + "; path=/";

    var h1 = document.cookie.indexOf("compare");

    if (h1 == -1)
    {
    getdiv1.style.display = "none";
    getdiv2.style.display = "none";
    }
    }

    In Opera, the cookies get deleted but for some odd reason the last cookie doesn't get deleted till after the function is done running so the div elements stay visible. Very Strange...Any help with this matter will be greatly appreciated. Thank you.

    -Isaac

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by 20sev20 View Post
    I'm basically hiding a couple of div elements when all cookies with the string "compare" have been deleted
    Does that refer to the name, value or both?

    Code:
    	document.cookie = item + expires + "; path=/";
    For this to work, the item parameter would need to be specified as "cookieName=cookieValue;"

    Code:
    	var h1 = document.cookie.indexOf("compare");
    This will pick up a name or a value - could that be the reason?
    Y_U U_G_A_E_U_ B_S_A_D_

  3. #3
    Join Date
    Aug 2010
    Posts
    3
    Hi Sterling,

    Thank you for your response. Sorry I tried to condense the entire javascript function to make it simpler to follow but it looks like I left out some key information

    1) It refers to the cookie name. Each cookie starts with the following:
    compare[id]=id
    The id within the brackets is replaced with the product id of the product we want to compare.

    2) Yes, the item parameter is specified as "CookieName=CookieValue" with the following code:

    var item = "compare" + id + "=id";
    document.cookie = item + "; path=/";

    id is a parameter passed to the function.

    3) It should only pick up a cookie name so once all cookies with the string like "compare" are deleted it should hide the div. It works in all browsers except Opera.

    Thanks for your help. Let me know if you want to see the entire JS function.

    -Isaac

  4. #4
    Join Date
    Jun 2007
    Posts
    667
    How do you tell that the last cookie isn't deleted until the function ends?
    What do you see if you do this?
    Code:
    alert( document.cookie );
    var h1 = document.cookie.indexOf("compare");

  5. #5
    Join Date
    Aug 2010
    Posts
    3
    This is where the problem becomes even stranger. Once the last cookie is removed, the alert shows the following:

    PHPSESSID=[random characters]

    So there are no cookies and the only thing remaining is the PhP session. Here's the strange part: When I added that line of code for the alert message, the function started working properly, as in the function hid the div elements after all the cookies were removed! Once I removed the alert code, it stopped working again. Weird right?

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Is it possible you need to wait for the DOM to partially (or completely) load before reading the available cookies?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles