Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Performing action once the next page has loaded?

I am currently building a google extension and this is what I'm going for:

Do something on awesome.page1.html then automatically press Next Page. When the new page has loaded, fill in a form's textfield.

My issue is that even though I know how to fill in the textfield, I don't quite know how to tell it to fill it in once page2 has loaded. It keeps trying to do everything on page 1.

This is what I'm trying but it's not working:

function addEffect() {
  document.getElementsByClassName("effect1 shine")[0].click();
  document.getElementsByClassName("nextPage BigButton")[0].click();
  nextStep();
}

function nextStep() {
  if(document.getElementsByClassName("myformTextField imageName") != undefined) { 
         alert('Page 2 is up.');
  } 

  else {
         alert('Page 1 is still up.');
         setTimeout("nextStep()", 250);
  }
}

I'm using an alert just for testing, and I keep getting the "Page 2 is up" even though it is still on page 1. I'm checking if an element, which is only present in page 2, is up. How could I make sure page2 is up?

like image 515
KingPolygon Avatar asked Jun 17 '13 20:06

KingPolygon


People also ask

How do I trigger a function after page load?

The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.

What are the ways to execute JavaScript after page load?

Method 1: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload event occurs whenever the element has finished loading. This can be used with the body element to execute a script after the webpage has completely loaded.

What event do you use to perform something after the page has finished loading?

The onload event occurs when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

How do you make JavaScript wait until the page is loaded?

How do you wait for DOM content to load? Native JS doesn't “disable” jQuery. In order to wait for the dom to be loaded, you can use window. onload=yourfunction.


2 Answers

The major issue you're going to run into is that JavaScript variables--including functions--don't persist from one page to another. Put another way, you can't write a function on one page and have it execute on the page that replaces it.

You could pass a URL variable or set a cookie for data persistence--or store settings on your server--but a straight JavaScript approach won't work.

Of course, there is a little trick that some folks use to load the entire DOM of the next page into a variable (using a variation on an XMLHttpRequest), apply the stored settings to the object in memory, and then replace most of the document body with most of the new DOM, but that's probably far more complicated than you need, and it has to conform to same-domain requirements.

like image 111
Neil JS Grump Avatar answered Sep 22 '22 12:09

Neil JS Grump


Well, your code here:

document.getElementsByClassName("myformTextField imageName")

returns an empty array when it finds nothing, which is not undefined. Therefor, your first if condition will always be true, regardless if it finds your item or not.

Instead, check the length returned by getElementsByClassName or use querySelector and check for null

if (document.getElementsByClassName("myformTextField imageName").length) { ... }

// Or..
if (document.querySelector('.myformTextField.imageName') !== null) { ... }
like image 27
rgthree Avatar answered Sep 22 '22 12:09

rgthree