Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Ben Os
Ben Os
20,008 Points

I have a Javascript function I run with Greasmonkey.. How could I call it only a single time per browser window?

I have a Javascript function I run with Greasmonkey. How could I call it only a single time per browser window?

For example:

  1. I open a browser, go to a certain webpage on which the function is working. The function runs once.
  2. I change the webpage to another, on the same browser session (window).
  3. I change the webpage back to a one in which the function is working but it won't work because it already worked only one time (stage 1).
  4. I close the browser session and open it again and repeat the process and again, the function runs only 1 time per session.

What should I add for this function, for example, so that it would run only once in the relevant webpage, per browser session?

document.addEventListener('DOMContentLoaded', function() {
    document.querySelector(".header-nav-item-sign-in").click();
}, false);

document.addEventListener('DOMContentLoaded', function() {
    document.querySelector("[type=submit]").click();
}, false);