title: Javascript, waiting for page to load after a redirect then executing code
body: <p>Hi I've seen this solution posted several times on this site and the answer usually is one of the following.</p>
  
  <pre><code>$( document ).ready()
  window.onload="myFunction()";
  </code></pre>
  
  <p>I'm making a google extension that redirects to a new page using chrome's tab update then using chrome's tab executescript to introduce a new script to that newly redirected page to click a button. I would redirect the page then using the above methods I tried to get the new script to run on the redirected page instead of the current page. Sadly none of them worked, they would still let the new script to be put in on the old page. So to get around this I just used setTimeout and set it to 1500, roughly enough time for me to load the new page, but that isn't very efficient and it might be different for users is there something I'm doing wrong or is this the only solution?</p>
  
  <p>My code looks something like this.</p>
  
  <pre><code>chrome.tabs.update({url: "www.example.com"});
  pause(); // I just took the liberty of shorting the whole setTimeout part
  chrome.tabs.executeScript(null, {file: "jquery.js"}), console.log("jquery loaded");
  </code></pre>
  
  <p>When I tried using the original solutions at the beginning of the page I'd just wrap the execute script in it to make it look something like this.</p>
  
  <pre><code>chrome.tabs.update({url: "www.example.com"});
  pause(); // I just took the liberty of shorting the whole setTimeout part
  $( document ).ready(){
    chrome.tabs.executeScript(null, {file: "jquery.js"}), console.log("jquery loaded");
  }
  </code></pre>
  
  <p>What I'm currently doing is using a short setTimeout of like 250 and then wrapping my code in .ready() which /works/ but I'd much rather remove setTimeout all together which is the point of this question.
  Thank you in advance! If you need more information just ask.</p>