I have noticed that the Javascript code is being loaded prior to the completion of my HTML/CSS page for some unknown

I am completely new to the world of web development. I'm encountering an issue where my JavaScript code (specifically alerts) is loading before my HTML/CSS page has fully loaded. I've been using sample alerts to test this out, and ideally, they should only display after the entire web page has finished loading. After doing some research on Google, I found that many people suggest placing the call to the Java Script file at the end of the HTML document, and that's exactly what I've done. However, the problem still persists.

    function go() {
alert('hi');
alert('hey there');
    }

    go();
<header> 
<nav>
<h1 class = "second"> RESUMAKER </h1>
<ul>
<li><a href="build.html" target="_blank"><span> Build Your Resume </span></a></li>
<li><a href="createaccount.html" target="_blank"><span> Create Account </span></a></li>
<li><a href="signin.html" target="_blank"><span> Sign in </span></a></li>
<li><a href="resources.html" target="_blank"><span> Resources </span></a></li>
<li><a href="contacts.html" target="_blank"><span> Talk To Us </span></a></li>
</ul>
</nav>
</header>

<h1 class = "third"><strong> What we do? </strong></h1>
<p> 1. We create your resume when you input information such as personal information, educational history, work experience and skills. </p>
<p> 2. The inputted information will be collected, stored, converted and in turn generate arrays of information which can be transformed into a resume.</p>
<p> 3. Then we let you select a predefined resume template all with different fonts and designs, to generate a resume. </p>
<p> 4. After all that is done, we allow you to be able to download generated resume as a PDF, Google or Microsoft Word document. </p>

<div class="footer">
  <li><a href="build.html" target="_blank"> Build a resume </a></li>
<li><a href="createaccount.html" target="_blank"> Create Account </a></li>
<li><a href="signin.html" target="_blank"> Sign in </a></li>
<li><a href="resources.html" target="_blank"> Resources </a></li>
<li><a href="contacts.html" target="_blank"> Contact us </a></li>
</div>

<div class="icon-bar">
<a href="#" class="facebook"><i class="fa fa-facebook"></i></a> 
<a href="#" class="twitter"><i class="fa fa-twitter"></i></a> 
<a href="#" class="google"><i class="fa fa-google"></i></a> 
<a href="#" class="linkedin"><i class="fa fa-linkedin"></i></a>
<a href="#" class="youtube"><i class="fa fa-youtube"></i></a> 
</div>

Answer №1

The occurrence can be explained by the fact that you have already initiated the go function, as indicated in your most recent instruction (go();). Consequently, the alert is triggered immediately upon script loading, potentially preceding the loading of your HTML/CSS content.

To remedy this situation, it is advisable to remove said directive and subsequently invoke the go function from within your HTML document.

Answer №2

If you want to delay the execution of a function, you can set a timeout. This approach will make the script wait for 20 milliseconds before running the `go` function. The issue here is that the alerts trigger as soon as the DOM is ready, without giving it any time to render. By using the following code snippet, you can see that the content renders first and then the alerts are shown.

var go = () => {
    alert('hi')
    alert('hey there')
}
window.addEventListener('load', async () => {
    await new Promise(resolve => {setTimeout(resolve, 20)})
    go()
})
<head>
    <title>Resumaker</title>
    <link rel="stylesheet" type="text/css" href="main.css">
    <!--<meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">-->
</head>

<body>

    <header> 
        <nav>
            <h1 class = "second"> RESUMAKER </h1>
            <ul>
                <li><a href="build.html" target="_blank"><span> Build Your Resume </span></a></li>
                <li><a href="createaccount.html" target="_blank"><span> Create Account </span></a></li>
                <li><a href="signin.html" target="_blank"><span> Sign in </span></a></li>
                <li><a href="resources.html" target="_blank"><span> Resources </span></a></li>
                <li><a href="contacts.html" target="_blank"><span> Talk To Us </span></a></li>
            </ul>
        </nav>
    </header>

    <h1 class = "third"><strong> What we do? </strong></h1>
    <p> 1. We create your resume when you input information such as personal information, educational history, work experience and skills. </p>
    <p> 2. The inputted information will be collected, stored, converted and in turn generate arrays of information which can be transformed into a resume.</p>
    <p> 3. Then we let you select a predefined resume template all with different fonts and designs, to generate a resume. </p>
    <p> 4. After all that is done, we allow you to be able to download generated resume as a PDF, Google or Microsoft Word document. </p>

    <div class="footer">
        <li><a href="build.html" target="_blank"> Build a resume </a></li>
        <li><a href="createaccount.html" target="_blank"> Create Account </a></li>
        <li><a href="signin.html" target="_blank"> Sign in </a></li>
        <li><a href="resources.html" target="_blank"> Resources </a></li>
        <li><a href="contacts.html" target="_blank"> Contact us </a></li>
    </div>

    <div class="icon-bar">
        <a href="#" class="facebook"><i class="fa fa-facebook"></i></a> 
        <a href="#" class="twitter"><i class="fa fa-twitter"></i></a> 
        <a href="#" class="google"><i class="fa fa-google"></i></a> 
        <a href="#" class="linkedin"><i class="fa fa-linkedin"></i></a>
        <a href="#" class="youtube"><i class="fa fa-youtube"></i></a> 
    </div>


    <script type="text/javascript" src="main.js"></script>

</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Get notified via email when submitting a form on a PHP contact form

I am a front-end developer and do not have experience with PHP. I am trying to set up a contact form on my website using an example from htmldog. Here is the current code snippet: <form action="contact.php" method="post"> <div class="c ...

Alert: Zone.js has identified that the ZoneAwarePromise '(window|global).Promise' has been replaced with polyfills. Kindly address this issue

Recently, I updated my Angular application from version 8 to 9. After updating the packages and successfully compiling the application, I encountered an error message in the Chrome browser console: Error: Zone.js has detected that ZoneAwarePromise `(wind ...

Accessing information from a json response using an ajax request

Currently, I am making an ajax call to fetch the longitude and latitude based on a pin code. Here is how I approached it: $(document).ready(function () { $.ajax({ type: "GET", url: "http://maps.googleapis.com/ma ...

The h3 element is not responding to the adjacent sibling selector

Below is my HTML DOM structure. I am trying to remove the margin and padding for all h3 elements that are immediate siblings of a div with the id "successor". I attempted to achieve this using the adjacent sibling selector "+", but unfortunately, I'm ...

Using $state.go within an Ionic application with ion-nav-view may cause unexpected behavior

I recently started working on an Ionic Tabs project. I have a button called "initiateProcess" that triggers some operations when clicked using ng-click. Within the controller, it performs these operations and then navigates to a specific state (tab.target) ...

Checking email existence through remote jQuery validation

Utilizing the jQuery validator plugin, I am implementing an ajax function with a remote method to validate whether an email already exists in my database. However, I am encountering an error when making the ajax call within my validation function. "email ...

Tips for delaying my response in nodejs until the loop is complete

This is the code I'm currently working with: myinsts.forEach(function (myinstId) { Organization.getOrgById(myinstId,function (err, insts) { res.json(insts); }) }); I am using Node.js and encountering an error message that says "Can't set hea ...

What is the best way to prevent a strikethrough from appearing on the delete button in my JavaScript TO-Do application?

I'm working on coding a to-do app using JavaScript, HTML, and CSS. The issue I'm facing is that when I add the text-decoration: line-through property to the list items, it also affects the X icon used for deleting tasks. I suspect this problem ar ...

Verifying a checkbox selection within an Autocomplete feature using MUI

[ { label: 'First', checked: false }, { label: 'Second', checked: true } ] Here is a brief example of how the data may be structured. I am utilizing Material UI's Autocomplete feature to enable searching based on labels. Thes ...

Deciphering the occurrence of jQuery-Mobile page firing events: the mystery behind dialog pages appearing upon closure

I'm still fairly new to jQuery-Mobile and I'm trying to wrap my head around what exactly happens when a page or dialog is loaded. To help illustrate the confusion I'm experiencing, I put together a small collection of files that showcase th ...

The result of Document.getElementById can show as "undefined" despite the presence of the element

Currently, I am tackling a project that involves extracting information from a website. I have opted to use the 'puppeteer' library in Node.Js for this task. However, I am encountering an issue where Document.getElementById is returning "undefine ...

Executing program through Socket.io alert

My NodeJS server sends notifications to clients when certain actions are performed, such as deleting a row from a grid. Socket.io broadcasts this information to all connected clients. In the example of deleting a row, one approach could be adding an `acti ...

Resolving Uncaught TypeError in React when trying to read properties of undefined (specifically 'map')

As a newcomer to React and API usage, I am facing difficulty resolving the error below. My goal is to showcase a carousel of images using React.js but encountered this issue: Error message: Popular.jsx:26 Uncaught TypeError: Cannot read properties of unde ...

Refresh two angular-datatables

I'm facing a challenge when it comes to updating two datatables simultaneously on the same page. Here's how my HTML is structured: <table id="datatable1" ng-if="Ctrl.dtOptions1" datatable="" dt-options="Ctrl.dtOptions1" dt-column-defs="Ctrl. ...

Extracting the value of *data* from my HTML and displaying it in the console with Javascript

I'm struggling to figure out what's going wrong with this code. I've done some research online and it seems like I should just include the window.onload = function() at the beginning of my code. However, no matter what I try, the value alway ...

Unexpected problem discovered with Firefox and JqueryUI - content is misaligned within the dialog box

I seem to be having an issue with a dialog that I recently set up. I've been working with jqueryUi for quite a while so I'm not sure why I can't figure this out. When using Firefox, I noticed that the dialog opens and closes when clicking " ...

When using jQuery, the search for the ID within an iframe may fail if the specified condition

I have a scenario where I need to dynamically generate an iframe and its corresponding id. Now, I need to check if the generated id already exists or not. This is what my code looks like: function createIframe(intxnId){ alert("The Id is : "+"$(&apo ...

Harnessing the power of service binding in AngularJS

I am utilizing a service to facilitate data sharing between two controllers and retrieve a list of data: myApp.service('DataService', function($http) { this.data = []; this.loadData = function() { $http.get('/api/data') ...

Issues with Angular ngroute: controllers are not functioning properly

In order to route my application with different themes on the same page, I plan to utilize the ngroute module. Here's an example of how I intend to achieve this: <html> <head> <title>Angular JS Views</title> ...

Hiding elements with CSS using the display:none property and using the :

Trying to display an image when hovering over another image. Works well in Safari, but Chrome and Firefox fail to load the image properly. Have searched for solutions related to visibility:hidden but none seem to solve this cross-browser issue. HTML being ...