Permalink
Browse files

renamed chrome bug example, added firefox bug example

  • Loading branch information...
1 parent f276cef commit ee5d6059707db0170e975d4e2a164361434552e4 @cowboy cowboy committed Nov 10, 2009
View
2 examples/back-button-test/index.php → examples/bug-chrome-back-button/index.php
@@ -2,7 +2,7 @@
include "../index.php";
-$shell['title3'] = "Back Button Test";
+$shell['title3'] = "Bug in Chrome: Back Button";
$shell['h2'] = 'Wanna see a strange issue in Chrome?';
View
222 examples/bug-firefox-remote-xhr/index.php
@@ -0,0 +1,222 @@
+<?PHP
+
+include "../index.php";
+
+$shell['title3'] = "Bug in Firefox: Remote XMLHttpRequest";
+
+$shell['h2'] = 'Wanna see a strange issue in Firefox?';
+
+// ========================================================================== //
+// SUBHEADER
+// ========================================================================== //
+
+ob_start();
+?><!--
+ <a href="http://benalman.com/news/2009/09/chrome-browser-history-buggine/">Read more about the issue here!</a>
+--><?
+$shell['h3'] = ob_get_contents();
+ob_end_clean();
+
+// ========================================================================== //
+// SCRIPT
+// ========================================================================== //
+
+ob_start();
+?>
+
+$(function(){
+
+ // Set the hash the "usual" way.
+ function callback_hash( idx ) {
+ debug.log( idx );
+ document.location.hash = '#done_' + idx;
+ };
+
+ // Set the hash by re-setting the entire URL.
+ function callback_href( idx ) {
+ debug.log( idx );
+ document.location.href = document.location.href.replace( /#.*$/, '' ) + '#done_' + idx;
+ };
+
+ // Set the hash by re-setting the entire URL.. asynchronously.
+ function callback_href_kluge( idx ) {
+ setTimeout(function(){
+ callback_href( idx );
+ }, 0);
+ };
+
+ var arr = [
+
+ // 1
+ [
+ 'JSON request, callback sets hash via location.hash',
+ {
+ url: '../../shared/json_echo.php?a=1',
+ success: function(){ callback_hash(1); }
+ }
+ ],
+
+ // 2
+ [
+ 'JSONP request, callback sets hash via location.hash',
+ {
+ url: 'http://api.flickr.com/services/feeds/photos_public.gne?id=8395214@N06&format=json',
+ dataType: 'jsonp',
+ jsonp: 'jsoncallback',
+ success: function(){ callback_hash(2); }
+ }
+ ],
+
+ // 3
+ [
+ 'JSON request, callback sets hash via location.href',
+ {
+ url: '../../shared/json_echo.php?a=1',
+ success: function(){ callback_href(3); }
+ }
+ ],
+
+ // 4
+ [
+ 'JSONP request, callback sets hash via location.href <i>(FAILS IN FIREFOX)</i>',
+ {
+ url: 'http://api.flickr.com/services/feeds/photos_public.gne?id=8395214@N06&format=json',
+ dataType: 'jsonp',
+ jsonp: 'jsoncallback',
+ success: function(){ callback_href(4); }
+ }
+ ],
+
+ // 5
+ [
+ 'JSON request, callback sets hash via location.href, setTimeout kluge used',
+ {
+ url: '../../shared/json_echo.php?a=1',
+ success: function(){ callback_href_kluge(5); }
+ }
+ ],
+
+ // 6
+ [
+ 'JSONP request, callback sets hash via location.href, setTimeout kluge used',
+ {
+ url: 'http://api.flickr.com/services/feeds/photos_public.gne?id=8395214@N06&format=json',
+ dataType: 'jsonp',
+ jsonp: 'jsoncallback',
+ success: function(){ callback_href_kluge(6); }
+ }
+ ]
+ ];
+
+ $.each( arr, function(i,v){
+ var idx = i + 1,
+ html = '<p><b>' + v[0] + '<\/b><\/p>'
+ + 'Click <a href="#">this link<\/a>.'
+ + ' The hash should immediately change to "start_' + idx + '", then "done_' + idx + '"'
+ + ' once the callback completes. Once done, press the back button once to see'
+ + ' "start_' + idx + '", and again to see an empty hash.';
+
+ $('<li/>')
+ .html( html )
+ .appendTo( '#nav' )
+ .find( 'a' )
+ .click(function(){
+ document.location.hash = '#start_' + idx;
+ $.ajax( $.extend({}, v[1]) );
+ return false;
+ });
+ });
+
+});
+<?
+$shell['script'] = ob_get_contents();
+ob_end_clean();
+
+// ========================================================================== //
+// HTML HEAD ADDITIONAL
+// ========================================================================== //
+
+ob_start();
+?>
+<script type="text/javascript" language="javascript">
+
+<?= $shell['script']; ?>
+
+$(function(){
+
+ // Syntax highlighter.
+ SyntaxHighlighter.highlight();
+
+});
+
+</script>
+<style type="text/css" title="text/css">
+
+/*
+bg: #FDEBDC
+bg1: #FFD6AF
+bg2: #FFAB59
+orange: #FF7F00
+brown: #913D00
+lt. brown: #C4884F
+*/
+
+#page {
+ width: 700px;
+}
+
+ol li {
+ padding-bottom: 1.2em;
+}
+
+ol li p {
+ margin-bottom: 0.2em;
+}
+
+ol li i {
+ color: #f00;
+}
+
+</style>
+<?
+$shell['html_head'] = ob_get_contents();
+ob_end_clean();
+
+// ========================================================================== //
+// HTML BODY
+// ========================================================================== //
+
+ob_start();
+?>
+
+<p>
+ First, open this page in a new window or tab, to clear any current window or tab history. Make your window wide enough so that you can see the full URL in the address bar, with some extra space left over.
+</p>
+
+<p>
+ Each link below, when clicked, should immediately change the location hash to "start_N" (where N is the index). Once the associated callback completes, the hash will be changed to "done_N". At this point, you should be able to press the browser "back" button once to see the "start_N" hash, and then a second time to see a completely empty hash.
+</p>
+
+<p>
+ Notice how #4 fails in Firefox! All six tests work in IE 8, Opera, Safari and Chrome (of course, Chrome is succeptible to <a href="../bug-chrome-back-button/">a totally separate history bug</a>), but this question remains: <strong>is this intentional Firefox behavior, or a bug?</strong>
+</p>
+
+<ol id="nav"></ol>
+
+<h3>The code</h3>
+
+<pre class="brush:js">
+<?= htmlspecialchars( $shell['script'] ); ?>
+</pre>
+
+<?
+$shell['html_body'] = ob_get_contents();
+ob_end_clean();
+
+// ========================================================================== //
+// DRAW SHELL
+// ========================================================================== //
+
+draw_shell();
+
+?>
View
68 shared/json_echo.php
@@ -0,0 +1,68 @@
+<?PHP?><?
+
+//
+// Simple PHP JSON/P Echo - "Cowboy" Ben Alman - http://benalman.com/
+// v1.0 - 2/20/2009
+//
+
+// Show script source if no params are passed
+if (count($_GET) == 0) {
+ highlight_file($_SERVER['SCRIPT_FILENAME']);
+ die;
+}
+
+// Get request params
+$obj = array();
+foreach ($_GET as $key => $value) {
+ $obj[$key] = $value;
+}
+foreach ($_POST as $key => $value) {
+ $obj[$key] = $value;
+}
+
+$json_string = isset($obj['JSON']) ? $obj['JSON'] : null;
+$jsonp_callback = isset($obj['callback']) ? $obj['callback'] : null;
+
+// remove misc unneeded params
+unset($obj['_']);
+unset($obj['callback']);
+unset($obj['JSON']);
+
+$json = $json_string ? $json_string : json_encode($obj);
+$jsonp = $jsonp_callback ? $jsonp_callback . "($json)" : $json;
+
+
+sleep(1); // simulate slow connection :D
+
+
+$is_xhr = isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
+ strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
+
+$is_referer = isset($_SERVER["HTTP_REFERER"]);
+
+if ($is_xhr) {
+
+ header( 'Content-type: application/json' );
+ print $jsonp;
+
+} else if ($is_referer) {
+
+ $params = array();
+ foreach ($obj as $key => $value) {
+ $params[] = urlencode($key) . '=' . urlencode($value);
+ }
+
+ $url = preg_replace('/\?.*$/', '', $_SERVER["HTTP_REFERER"]);
+ $url .= '?' . implode('&', $params);
+
+ header( "Location: $url");
+
+} else {
+
+ header( 'Content-type: text/plain' );
+
+ print $jsonp;
+
+}
+
+?>

0 comments on commit ee5d605

Please sign in to comment.