0

I want to load stackoverflow page and then raise the alert, strictly one after the other, without using frameworks like jQuery etc. I have gone through the answers here and visited this too.

I ran the following in browser console. The page loads but the alert is not raised. I am using chrome in windows 8.1.

Try #1:

window.location.href = 'https://stackoverflow.com/';
window.onload = function () { alert("It's loaded!") }

Try #2:

window.location.href = 'https://stackoverflow.com/';
if(document.readyState === "complete") {
  //Already loaded!
  window.onload = function () { alert("It's loaded!") }
}
else {
  //Add onload or DOMContentLoaded event listeners here: for example,
  window.addEventListener("onload", function () {/* your code here */}, false);
  //or
  //document.addEventListener("DOMContentLoaded", function () {/* code */}, false);
}

Try #3:

window.location.href = 'https://stackoverflow.com/';
var everythingLoaded = setInterval(function() {
  if (/loaded|complete/.test(document.readyState)) {
    clearInterval(everythingLoaded);
    alert("It's loaded!");
  }
}, 1000);

Try #4:

Tried setTimeout() too but doesn't work either.

I have tried above examples with window.location.replace() also.

How do I make this work?

P.S: I am novice with javascript. The above codes are not mine but I am just trying to work them out. I don't claim to have understood them completely either.

Community
  • 1
  • 1
Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57
  • 1
    I don't think anything like this can work. When you load another page into the window, all the scripts from the current page stop running. – Barmar Mar 15 '17 at 20:36
  • What is your goal? Some things may or may not work depending. – Marc Rohloff Mar 15 '17 at 21:11
  • @MarcRohloff: My goal is something similar to enter username and click 'next' on gmail login page. After the page that asks for password has completely loaded then enter password and submit. All this to do in javascript. I am not able to raise alert after clicking 'next'. – Shridhar R Kulkarni Mar 16 '17 at 10:06
  • I'm pretty sure that gmail has restrictions against being loaded in a domain. You could try: `var popup = window.open("http://stackoverflow.com/"); popup.onload = function () { alert("It's loaded!") };` but that will probably be prevented by a popup blocker. – Marc Rohloff Mar 16 '17 at 15:41
  • @MarcRohloff: I tried on other site with simple html form but that doesn't work either. – Shridhar R Kulkarni Mar 16 '17 at 16:06
  • If what you want to do is get a user to authenticate using Google then you might want to look into their OAuth 2 'api' – Marc Rohloff Mar 16 '17 at 19:32
  • @Marc Thanks for the info. I want to learn this for generic purpose not just google logins. – Shridhar R Kulkarni Mar 17 '17 at 06:28
  • The thing to learn out of this is that browsers are very restrictive about what they allow you to do. There is a lot of effort put into making sure that a website or server, say stackoverflow, cannot be manipulated by code on another page. That applies in many senses from local and remote code execution to loading in an iframe and accessing server APIs (ajax). What you are trying to do is not possible in a generic way (there are ways around it but only if the destination web-page provides a way. – Marc Rohloff Mar 17 '17 at 14:58

1 Answers1

0

As Barmar pointed out this is not possible. The new page load will remove all JavaScript from the old page. Some alternatives are:

  • You can open the new page or in a new tab using the window.open api and attach an event handler.

  • You can load the page inside a frame (if it doesn't have any restrictions preventing that) and add a load event to the frame.

  • You can load the page using XMLHttpRequest (or a library of your choice) and insert the result into a <div> though not all of it will probably work.

You don't say what you want to do once the page has loaded. In most cases (generally unless you own the second page) it will not be possible to access any information on the second page.

Marc Rohloff
  • 1,332
  • 7
  • 8
  • 1
    `XMLHttpRequest` won't work if it's not the same domain, unless the server sends an `Access-Control-Allow-Origin` header. – Barmar Mar 15 '17 at 21:26