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

Automatic login to Treehouse script causes recursive loop

2 Answers

I published a solution in the question there in StackOverflow.

It turns out I used a unfitted class (instead a combination of two classes) to initiate logging in for a non connected user.

This is the correct code for automatic login to Treehouse (as long as Treehouse doesn't keep a user logged in):

if ( document.documentElement.classList.contains("visitor") && document.body.id != "signin" ) {
    window.location.href = "https://teamtreehouse.com/signin";
}

if (window.location.href.match("https://teamtreehouse.com/signin")) {
    setTimeout (function() {
        document.querySelector("button[type=submit]").click();
    }, 2000);
}
Steven Parker
Steven Parker
243,656 Points

I don't know what browser or plug-in you are using, but assuming that script gets re-executed on each page load, it would click the first submit control on the page 1 second later. And if that should cause the same page to be re-loaded, well that would explain the loop.

You might need to enclose that setTimeout in a conditional to prevent it from being set again.