Skip to content
Fix Code Error

How to run a function when the page is loaded?

March 13, 2021 by Code Error
Posted By: Anonymous

I want to run a function when the page is loaded, but I don’t want to use it in the <body> tag.

I have a script that runs if I initialise it in the <body>, like this:

function codeAddress() {
  // code
}
<body onLoad="codeAddress()">

But I want to run it without the <body onload="codeAddress()"> and I have tried a lot of things, e.g. this:

window.onload = codeAddress;

But it is not working.

So how do I run it when the page is loaded?

Solution

window.onload = codeAddress; should work – here’s a demo, and the full code:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
        function codeAddress() {
            alert('ok');
        }
        window.onload = codeAddress;
        </script>
    </head>
    <body>
    
    </body>
</html>

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
        function codeAddress() {
            alert('ok');
        }
        
        </script>
    </head>
    <body onload="codeAddress();">
    
    </body>
</html>
Answered By: Anonymous

Related Articles

  • Reference - What does this regex mean?
  • Difference between variable declaration syntaxes in…
  • Is it possible to apply CSS to half of a character?
  • adding to window.onload event?
  • “tag already exists in the remote" error after…
  • How to filter a RecyclerView with a SearchView
  • Fix top buttons on scroll of list below
  • Function is not defined - uncaught referenceerror
  • Git merge with force overwrite
  • useEffect Error: Minified React error #321 (GTM…
  • show all tags in git log
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Ukkonen's suffix tree algorithm in plain English
  • For-each over an array in JavaScript
  • What's the difference between eval, exec, and compile?
  • Smart way to truncate long strings
  • loop and eliminate unwanted lines with beautiful soup
  • How does PHP 'foreach' actually work?
  • What is a NullReferenceException, and how do I fix it?
  • data.table vs dplyr: can one do something well the…
  • Polymer 2.0 nested iron-pages
  • What are the undocumented features and limitations…
  • Why doesn't the height of a container element…
  • What is your most productive shortcut with Vim?
  • Pure JavaScript equivalent of jQuery's $.ready() -…
  • jQuery Mobile: document ready vs. page events
  • Usage of __slots__?
  • What is git tag, How to create tags & How to…
  • Why does C++ code for testing the Collatz conjecture…
  • Can I run javascript before the whole page is loaded?
  • beginner with vue.js treeview
  • Why do I have to "git push --set-upstream origin "?
  • JSP tricks to make templating easier?
  • How to paste yanked text into the Vim command line
  • How to use html template with vue.js
  • Identifying and solving…
  • What's the difference between including files with…
  • load and execute order of scripts
  • What are the new features in C++17?
  • How to detect if multiple keys are pressed at once…
  • What does "Fatal error: Unexpectedly found nil while…
  • What does do?
  • window.onload vs document.onload
  • How to generate a random string of a fixed length in Go?
  • C++, What does the colon after a constructor mean?
  • ExpressJS How to structure an application?
  • How do I include a JavaScript file in another…
  • commandButton/commandLink/ajax action/listener…
  • Close Window from ViewModel
  • Why do git fetch origin and git fetch : behave differently?
  • How to recover stashed uncommitted changes
  • Playing HTML5 video on fullscreen in android webview
  • Load and execution sequence of a web page?
  • My NSWindow's shadow is getting cut off when…
  • How to use Servlets and Ajax?
  • React client tries to access server resource without…
  • HTML table with 100% width, with vertical scroll…
  • Getting a loading scren to work with Polymer
  • C++ OpenGL stb_image.h errors
  • How to detect JS frameworks/libraries used in a page?
  • Examples of GoF Design Patterns in Java's core libraries
  • What is an optional value in Swift?
  • How can I condense this Python Tkinter GUI input code?
  • Typeerror: n is undefined in underscore.min.js
  • How does the "this" keyword work?
  • Checkout another branch when there are uncommitted…
  • Vue&TypeScript: how to avoid error TS2345 when…
  • Polymer paper-dialog: how can I know when an…
  • How to include a CDN to VueJS CLI without NPM or Webpack?
  • Web Scraping child classes or cleaning up html returned
  • Google Forms file upload complete example
  • Best practice multi language website
  • iron-collapse opened=false not collapsing when bound…
  • Ways to circumvent the same-origin policy
  • RegEx match open tags except XHTML self-contained tags
  • What does a "Cannot find symbol" or "Cannot resolve…
  • How to get the updated entry string from a toplevel…
  • vue js cant understand keep alive
  • binding ajax response to vue.js (multiple requests…
  • Best way to extract messy HTML tables using BeautifulSoup
  • Geolocation denied for HTML embedded site - anchor…
  • how to toggle class for multiple elements in vue js
  • How to remove a child component with a delete button…
  • How can I find the product GUID of an installed MSI setup?
  • In plain English, what does "git reset" do?
  • "Thinking in AngularJS" if I have a jQuery background?
  • Bootstrap Carousel Full Screen
  • Understanding PrimeFaces process/update and JSF…
  • How to download and save an image in Android
  • In CSS Flexbox, why are there no "justify-items" and…
  • How to set the Google Analytics cookie only after…
  • How to separate opening and closing tag by…
  • PHP parse/syntax errors; and how to solve them
  • Vuetify Navigation Drawer works once then stops
  • Select Tag Helper in ASP.NET Core MVC
  • enum to string in modern C++11 / C++14 / C++17 and…
  • default value selection in bootstrap multiselect…
  • Replacing a 32-bit loop counter with 64-bit…
  • How do Mockito matchers work?
  • What's the difference between HEAD^ and HEAD~ in Git?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

What does a “Cannot find symbol” or “Cannot resolve symbol” error mean?

Next Post:

Command to list all files in a folder as well as sub-folders in windows

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error