Hack Forums
[Help] Displaying view HTML/Javascript - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Help] Displaying view HTML/Javascript (/showthread.php?tid=5790305)



Displaying view HTML/Javascript - rafflan1891 - Jan 25, 2018

Hello!

I have this: https://pastebin.com/jGZW6kDK and I can register a new user, but when I sign in I get: Uncaught TypeError: Cannot read property 'style' of null on line 125. This is the HTML: https://pastebin.com/61bacbcT for the page you should come to after you have signed in. But as you see, the name of the outer div(which contains everything else there in the body) is the same as the name on line 125 in my first paste. But why do I still get that error?

I have tried to restart browser and clear everything etc.


RE: Displaying view HTML/Javascript - Charger - Jan 25, 2018

The js is probably running before the html is there.


RE: Displaying view HTML/Javascript - Nachfolger - Jan 25, 2018

As Charger mentioned, the javascript is probably running before the page is fully loaded. Use a timeout if possible, I didn't read your entire script so I'm not sure if it's gonna effect a ladder of things or not.

Two options: timeout | throw the entire page in a document load event
Code
// Will execute 250ms (0.2s after the script is loaded)
setTimeout(function() { // JAVASCRIPT ERROR CAUSING LINE HERE }, 250);

Code
Document load event (to lazy to write it out, but you can google it)



RE: Displaying view HTML/Javascript - rafflan1891 - Jan 25, 2018

(Jan 25, 2018 03:19 PM)rafflan1891 Wrote:  Hello!

I have this: https://pastebin.com/jGZW6kDK and I can register a new user, but when I sign in I get: Uncaught TypeError: Cannot read property 'style' of null on line 125.  This is the HTML: https://pastebin.com/61bacbcT for the page you should come to after you have signed in. But as you see, the name of the outer div(which contains everything else there in the body) is the same as the name on line 125 in my first paste. But why do I still get that error?

I have tried to restart browser and clear everything etc.

(Jan 25, 2018 03:25 PM)Charger Wrote:  The js is probably running before the html is there.

(Jan 25, 2018 03:31 PM)Nachfolger Wrote:  As Charger mentioned, the javascript is probably running before the page is fully loaded. Use a timeout if possible, I didn't read your entire script so I'm not sure if it's gonna effect a ladder of things or not.

Two options: timeout | throw the entire page in a document load event
Code
// Will execute 250ms (0.2s after the script is loaded)
setTimeout(function() { // JAVASCRIPT ERROR CAUSING LINE HERE }, 250);

Code
Document load event (to lazy to write it out, but you can google it)

Alright but I have my javascript in the end of my html, so how could that be a problem? As you see here, the script tag is at the end of the body: https://pastebin.com/211qvJvT

In my other html page(which is the homepage), I also have it in the end of the body: https://pastebin.com/7PVHHQFJ


RE: Displaying view HTML/Javascript - Cød3ing - Jan 26, 2018

Do not use a setTimeout, it's a synchronous method and it's bad practice to define a constant variable for unpredictable behavior like page load times.

JS is executed after the page has loaded, there's a lot of DOM APIs available for you to use in order to hook onto the event handler for the browsers DOM.

Take a look at https://developer.mozilla.org/en-US/docs/Web/Events/load


RE: Displaying view HTML/Javascript - rafflan1891 - Jan 26, 2018

(Jan 26, 2018 02:27 AM)Cod3ing® Wrote:  Do not use a setTimeout, it's a synchronous method and it's bad practice to define a constant variable for unpredictable behavior like page load times.

JS is executed after the page has loaded, there's a lot of DOM APIs available for you to use in order to hook onto the event handler for the browsers DOM.

Take a look at https://developer.mozilla.org/en-US/docs/Web/Events/load

Okay I tried changing my listeners from click to load, but now I get: "favicon.ico Failed to load resource: the server responded with a status of 404 (File not found)" in the Console in my browser, and I don't quite understand this error. Haven't done anything with favicon.ico


RE: Displaying view HTML/Javascript - Uose - Jan 26, 2018

Code
document.getElementById("test").style="display:none";
try

where your html code will look like this:

Code
<div style="display:inline-block" id="test"></div>



RE: Displaying view HTML/Javascript - rafflan1891 - Jan 26, 2018

(Jan 26, 2018 10:02 AM)Uose Wrote:  
Code
document.getElementById("test").style="display:none";
try

where your html code will look like this:

Code
<div style="display:inline-block" id="test"></div>

I tried that, now I don't get that error message but nothing happens when I log in. PM:ed you and asked if I could zip my folder and then it is easier to show you if you can open the homepage by yourself and try register locally.