/
code.html
82 lines (73 loc) · 3.72 KB
/
code.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>History API / Code — Warpspire Experiments</title>
<link rel="stylesheet" type="text/css" href="../css/proxima.css" />
<link rel="stylesheet" type="text/css" href="css/history_api.css" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-146426-5']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div id="experiments-header">
<div class="inner">
<h2 class="logo"><a href="/experiments/">Warpspire Experiments</a></h2>
<p class="back">In relation to <a href="http://warpspire.com/talks/responsive/">Responsive web design from the future</a></p>
</div>
</div><!-- /#experiments-header -->
<div id="content" class="columns typical">
<ul class="column navigation">
<li><a href="/experiments/history-api/" class="overview-style">Overview</a></li>
<li><a href="/experiments/history-api/explanation" class="explanation-style">Explanation</a></li>
<li><a href="/experiments/history-api/code" class="code-style selected">See the Code</a></li>
</ul><!-- /.column.navigation -->
<div class="column main">
<h1>The Code</h1>
<p>
You can see the full code for this example <a href="https://github.com/kneath/kneath.github.com/tree/master/experiments/history-api">on
the GitHub repo for this blog</a>. Most of the code is supporting Javascript —
I'm using <a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a> for the app code,
jQuery to do DOM Manipulation, JSONP (via <a href="http://api.jquery.com/jQuery.getJSON/">$.getJSON</a>) to retreive the tweets from
<a href="http://dev.twitter.com/doc/get/statuses/user_timeline">Twitter's API</a>,
<a href="http://handlebars.strobeapp.com/">handlebars.js</a> to format the tweets, and
<a href="https://github.com/twitter/twitter-text-js">twiter_text.js</a> to
autolink usernames, links and hashtags.
</p>
<p>The interesting bits are in <a href="https://github.com/kneath/kneath.github.com/blob/master/experiments/history-api/javascripts/app/twitter_timeline.coffee">twitter_timeline.coffee</a>:</p>
<script src="https://gist.github.com/998549.js?file=twitter_timeline.coffee"></script>
<p>
In particular, the <code>didScroll</code> method should be interesting. As we
scroll, we're checking to see if we should permalink the page. When we call
<code>permalink</code>, we use the History API <code>replaceState</code>
method to change the URL.
</p>
<p>
On the first page load the <code>TwitterTimeline</code> constructor is called
and I parse out any <code>max_id</code> in the URL to fetch tweets later than that point in time.
This makes the page "sticky" when you exit the page and come back to it via the back button.
</p>
</div><!-- /.column.main -->
</div><!-- /#content -->
<script type="text/javascript">
(function() {
var t = document.createElement('script');
t.type = 'text/javascript';
t.async = true;
t.id = 'gauges-tracker';
t.setAttribute('data-site-id',
'4dd8683cf5a1f53796000001');
t.src = '//secure.gaug.es/track.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(t, s);
})();
</script>
</body>
</html>