1

Edit: This only happens in firefox, it works fine in chrome.

Edit 2: Due to there apparently not being a solution to this (sessionid breaks when there are other cookies present) i've decided to use localstorage instead (it's also a much better approach)

I have an audio player on my website (website powered by django) and I want to store the current time, the source and the state (is it playing or not) in cookies. So when you refresh the page while music is playing, it'll continue where you left off. I have a timer set to update the cookie track_time every second. And it works, however:

When you try logging into the website a second time, while audio is playing, it won't let you. It says in my console that the login happened, but firefox doesn't seem to store the session. When I disable the script that is writing the cookies, it works again.

Here's proof:

http://puu.sh/nkqIX/23160ce67e.png

What in the world is happening here? I'm not recieving any errors, it just doesn't work.

Code: This snippet here creates, reads, and deletes cookies.

/** Cookies **/

function createCookie(name, value, days) {
    'use strict';
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

In audio.js I have the following functions:

// Cookie? //

function audioCookie() {
    'use strict';
    var track, track_time, track_state;
    track = readCookie('track');
    track_time = readCookie('track_time');
    track_state = readCookie('track_state');
    if (track !== null) {
        track = JSON.parse(track);
        audioCurrent = new AudioTrack(track.src, track.artist, track.title, track.genre, track.type);
        audioCurrent.setSrc();
        if (track_state === '1') {
            audioCurrent.play();
        }
        setTimeout(function () {
            audioCurrent.time(track_time);
            audioSetProperties();
            audioViewUpdate();
            audioElementUpdatePlayButton();
            audioAnalyserInitialize();
        }, 250);
    }
}

And

var audioTimeUpdate = function () {
    'use strict';
    if (audioSource.paused === false) {
        audioSetProperties();
        createCookie('track_time', audioSource.currentTime, 360);
        setTimeout(function () {
            audioTimeUpdate();
        }, 500);
    }
};

Create the cookie containing the AudioTrack object (source, name, etc)

// Play the track that is being viewed

var audioPlayFromView = function () {
    'use strict';
    audioCurrent = audioFromView;
    var audioCurrentString = JSON.stringify(audioCurrent);
    createCookie('track', audioCurrentString, 360);
    audioCurrent.setSrc();
    audioCurrent.play();
    if (analyserInitialized === true) {
        source.disconnect();
        source = context.createMediaElementSource(audioSource);
    }
    audioViewUpdate();
};
Community
  • 1
  • 1
Sebastian Olsen
  • 10,318
  • 9
  • 46
  • 91

0 Answers0