title: Google Chrome Extension for blocking redirects
body: <p>Using Chrome Extension, I want to stop a Webpage from redirecting to another page (Different URL). I need to stop the redirects but without displaying any alert message/popups. so when this extension is 'enabled' on google chrome. It will do the work of blocking the re-directs. Once the page loads fully, i.e. all the scripts, HTML are loaded etc only then 'ESC' or stop by default trigger. If not 'ESC' then some other way to stop re-direct without any pop-up or alerts.</p>
  
  <p>As part of this, the example HTML code and my JS code reads:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var seconds = 3; // seconds for HTML
  var foo; // variable for clearInterval() function
  
  function redirect() {
      document.location.href = 'http://bubencode.com';
  }
  
  function updateSecs() {
      document.getElementById("seconds").innerHTML = seconds;
      seconds--;
      if (seconds == -1) {
          clearInterval(foo);
          redirect();
      }
  }
  
  function countdownTimer() {
      foo = setInterval(function () {
          updateSecs()
      }, 1000);
  }
  
  countdownTimer();
  
  window.onbeforeunload = function() {return "Stop";}; //Alert  </code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p&gt;You should be automatically redirected in &lt;span id="seconds"&gt;3&lt;/span&gt; seconds.
  &lt;/p&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am facing the following two issues or need help in:</p>
  
  <ol>
  <li><code>window.onbeforeunload = function() {return "Stop";};</code> 
  is not working.</li>
  <li>Is there an alternate way to stop this redirect process with no browser alerts or pop-ups? I have used the following ESC keycode (27) but it also fails:</li>
  </ol>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function() {
      var e = $.Event('keypress');
      e.which = 27
      $('document').trigger(e);
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>I want to implement a chrome extension to automatically take care of this.
  I need help in  javascript code. The HTML code mentioned is just for the example.
  I'm sorry if the question here is irrelevant. Please excuse me for the same.</p>