<tr>
     <th class="align-middle" scope="row">Thread:</th>
     <td>
-     <select id="thread_select" class="custom-select" onchange="onThreadSelect()">
+     <select id="thread_select" class="custom-select">
   {%for m in threadstruct%}{%if m.id%}<option value="{{m.messageid|urlencode}}"{%if m.id == msg.id%} selected="selected"{%endif%}>{{m.indent|safe}}{{m.printdate}} from {{m.mailfrom|hidemail}}{% if m.hasattachment %}        📎{% endif %}</option>{%endif%}
   {%endfor%}
      </select>
 
     <script src="/media-archives/js/popper.min.js?{{gitrev}}"></script>
     <script src="/media-archives/js/bootstrap.min.js?{{gitrev}}"></script>
     <script src="/media-archives/js/main.js?{{gitrev}}"></script>
-    <script type="text/javascript">
-      var _gaq = _gaq || [];
-      _gaq.push(['_setAccount', 'UA-1345454-1']);
-      _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>
-    <script type="text/javascript">
-      var shiftWindow = function() { scrollBy(0, -80) };
-      if (location.hash) shiftWindow();
-      window.addEventListener("hashchange", shiftWindow);
-    </script>
   </body>
 </html>
 
 {%extends "page.html"%}
 {%block title%}{%if not msg.hiddenstatus%}{{msg.subject}}{%endif%}{%endblock%}
 
-{%block extrahead%}
-<script type="text/javascript">
-function onThreadSelect() {
-   document.location.href = '/message-id/' + document.getElementById('thread_select').value;
-}
-
-$(function(){
-   $('select#thread_select').selectmenu({
-      style:'dropdown',
-      escapeHtml:true,
-      icons:[
-         {find:'.hasatt', icon: 'ui-icon-document'},
-      ]
-   });
-});
-</script>
-{%endblock%}
-
 {%block contents%}
 {%if not msg.hiddenstatus%}
 <h1 class="subject">{{msg.subject}}</h1>
 
 {%load pgfilters%}
 {%block extrahead%}
 <meta name="robots" content="noindex" />
-{%if not isfirst%}
-<script type="text/javascript">
-function load() {
-   if (document.location.href.indexOf('#') < 0) {
-      document.location.href = document.location.href + '#' + '{{msg.messageid|md5}}';
-   }
-}
-window.onload = load;
-</script>
-{%endif%}
 {%endblock%}
 {%block contents%}
 {%if not msg.hiddenstatus%}
-<h1 class="subject">{{msg.subject}}</h1>
+<h1 id="flatMsgSubject" class="subject" data-isfirst="{{isfirst}}" data-msgid="{{msg.messageid|md5}}">{{msg.subject}}</h1>
 <table class="table-sm table-responsive message-header" style="border-bottom: 0.05em solid #E7EAE8; margin-bottom: 1em;">
  <tr>
   <th scope="row" style="width: 10%">Lists:</th>
 
     $(".navbar").toggleClass("compressed", $(window).scrollTop() >= 20);
   });
 });
+
+
+$(function(){
+    /* Callback from main message view when a message is picked in dropdown */
+    $('#thread_select').change(function(e) {
+       document.location.href = '/message-id/' + $(this).val();
+    });
+
+
+    /*
+     * For flat message view, redirect to the anchor of the messageid we're watching,
+     * unless we happen to be the first one.
+     */
+    $('#flatMsgSubject[data-isfirst=False]').each(function(i, e) {
+       if (document.location.href.indexOf('#') < 0) {
+           document.location.href = document.location.href + '#' + $(e).data('msgid');
+           return;
+       }
+    });
+});
+
+
+
+/*
+ * Google analytics
+ */
+var _gaq = _gaq || [];
+_gaq.push(['_setAccount', 'UA-1345454-1']);
+_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);
+})();