Page 1 of 1

Browser back button blues...

Posted: Sat Aug 01, 2009 1:31 pm
by mscrilla
Hey everybody,
So I have this site that generates xml playlists for a user and plays them in a JW Flash player. I'd like it to behave such that, when a user navigates away from the player page (like closes the window, or goes to another site) or when they click the back button on their browser that the playlist is deleted. So there's 2 pages: 1) A set up page with checkboxes that dictates what kind of playlist will be generated and 2) the player page. On the first page, I have a method called init() which clears all the checkboxes and tries to delete any playlist that exists. On the second page, I try to delete the playlist if they navigate away. Here's the code for init() :

Code: Select all

 
function init() {
            
                //This variable maps a supergenre to an array of its subgenres
                superToSubs = new Array();
                
                var str = "<?php echo $genreString; ?>";    
                var superToSubString = str.split("!!");
 
                for (var i = 0; i < superToSubString.length; i++) {
                    var superSubSplit = superToSubString[i].split("$$");
                    //position 0 has the super, and then position one has SUB**SUB**SUB**
                    var subs = null;
                    if (superSubSplit.length > 1)
                        subs = superSubSplit[1].split("**");
                    superToSubs[superSubSplit[0]] = subs;
                
                }
                
                var elements = document.getElementsByTagName('*');
                for (var i = 0; i < elements.length; i++) {
                    if (elements[i].type == 'checkbox')
                        elements[i].checked = false;
                }
                
                document.getElementById('random').checked = true;
            
                var dummyimage = new Image();
                dummyimage.src = "deletePlaylist.php";
            
            }
 
So that deletePlaylist.php is the file that looks for the playlist, here's its code:

Code: Select all

 
<?php 
      $fh ="playlist.xml";
      unlink($fh);
?>
 
So I'm pretty sure that's not the best way to call an external php file in javascript, so that's my first question. What's the proper way to do that? Now the biggest issue is that, I notice when using Firebug, when I click 'back' on my browser from the player page, it doesn't execute the 'init()' method. I've tried placing headers in a php script at the top of the page to force the browser to not cache it:

Code: Select all

 
        header('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified', gmdate('D, d M Y H:i:s').' GMT');
    header('Cache-Control', 'no-store, no-cache, must-revalidate');
    header('Cache-Control', 'post-check=0, pre-check=0');
    header('Pragma', 'no-cache');
 
But that doesn't seem to change the behavior. I'm wondering what's the proper way to change the browser page from the checkbox page to the player page so that the back button behaves properly. The way I currently change pages is that when the user clicks submit, it goes to a php page which simply says:

Code: Select all

 
    header('Location: sikhpod.html');
 
Where 'sikhpod.html' is the player page. The way that I ask this 'sikhpod.html' page to delete playlists is by using this event:

Code: Select all

 
      function closeBrowser() {
        
        var dummyimage = new Image();
        dummyimage.src = "deletePlaylist.php";
        //alert("HI!");
      }
      
    </script>
</head>
<body onload='showlist(300);' onunload='closeBrowser()' onbeforeunload='closeBrowser()'>
 

I figured this would be the best place to post to find out about the best way to redirect in php...can anybody offer some advice for a php newbie? I'd really appreciate it.

Thanks,
Mark