Advertisement
5ally

Bootstrap tabs with hashchange support

Mar 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.29 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">
  7.     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  8.     <meta name="description" content="">
  9.     <meta name="author" content="">
  10.     <!--<link rel="icon" href="../../favicon.ico">-->
  11.  
  12.     <title>Bootstrap tabs with hashchange support</title>
  13.  
  14.     <!-- Bootstrap core CSS -->
  15.     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  16.  
  17.     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  18.     <!--[if lt IE 9]>
  19.      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  20.      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  21.    <![endif]-->
  22.   </head>
  23.  
  24.   <body>
  25.  
  26.     <div class="container">
  27.  
  28. <div class="page-header">
  29.   <h2>
  30.     Bootstrap tabs demo
  31.     <small>by <a href="https://stackoverflow.com/users/9217760/sally-cj">Sally</a></small>
  32.   </h2>
  33. </div>
  34.  
  35. <div class="alert alert-info">
  36.   <p>
  37.     <strong>Note:</strong>
  38.     The markup/<code>HTML</code> was copied from <a href="https://getbootstrap.com/docs/3.3/javascript/#tabs">this page</a>, and this template was based on <a href="https://getbootstrap.com/docs/3.3/examples/starter-template/">the Starter Template for Bootstrap 3</a>.
  39.   </p>
  40. </div>
  41.  
  42. <p>
  43.   Click on the <a href="#profile">Profile link</a> and press the <kbd>Back</kbd> button on your keyboard. And/or modify the URL <i>hash</i> &mdash; i.e. add <code>#profile</code> to the URL address in the browser's address box/bar &mdash; and see if the <code>onhashchange</code> callback works properly. Then try with the other links (e.g. <a href="#settings">Settings</a>) and/or hashes (e.g. <code>#settings</code>).
  44. </p>
  45.  
  46. <div>
  47.  
  48.   <!-- Nav tabs -->
  49.   <ul class="nav nav-tabs" role="tablist">
  50.     <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
  51.     <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
  52.     <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
  53.     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
  54.   </ul>
  55.  
  56.   <!-- Tab panes -->
  57.   <div class="tab-content">
  58.     <div role="tabpanel" class="tab-pane active" id="home"><p>Content for the Home tab.</p></div>
  59.     <div role="tabpanel" class="tab-pane" id="profile"><p>Content for the Profile tab.</p></div>
  60.     <div role="tabpanel" class="tab-pane" id="messages"><p>Content for the Messages tab.</p></div>
  61.     <div role="tabpanel" class="tab-pane" id="settings"><p>Content for the Settings tab.</p></div>
  62.   </div>
  63.  
  64. </div>
  65.  
  66.     </div><!-- /.container -->
  67.  
  68.  
  69.     <!-- Bootstrap core JavaScript
  70.    ================================================== -->
  71.     <!-- Placed at the end of the document so the pages load faster -->
  72.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  73.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  74.  
  75.     <script>
  76. // Forked from https://codepen.io/rnr/pen/dMNZmx/
  77. $(function(){
  78.   var defaultTab = '#home';
  79.  
  80.   function hashChanged() {
  81.     var hash = window.location.hash || defaultTab,
  82.       $link = $('.nav-tabs a[href="' + hash + '"]');
  83.  
  84.     if ( $link.size() ) {
  85.       $link.focus(); // Remove or comment out this if you don't want to focus on the link.
  86.       $link.tab('show');
  87.     }
  88.   }
  89.  
  90.   $('.nav-tabs a').click(function (e) {
  91.     this.blur(); // Remove or comment out this if you want the link to be kept focused.
  92.     $(this).tab('show');
  93.     var scrollmem = $('body').scrollTop();
  94.     window.location.hash = this.hash;
  95.     $('html,body').scrollTop(scrollmem);
  96.   });
  97.  
  98.   $( window ).on( 'load hashchange', function( e ) {
  99.     /*if ( 'load' === e.type && ! window.location.hash ) {
  100.      // First time load; no hash; no need to re-show the default tab. =)
  101.       return;
  102.     }*/
  103.  
  104.     hashChanged();
  105.   } );
  106. });
  107.     </script>
  108.   </body>
  109. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement