How can I run javascript code just after page load, when page is loaded, it trigger, and when I go to another page it also trigger another piece of code for that page, how can I do it ?
Asked
Active
Viewed 3.0k times
-3
-
`window.onload = function() { // do something }` – Weafs.py Dec 24 '14 at 08:34
-
read up jquery `$(document).ready()` – Aditya Dec 24 '14 at 08:35
-
1@Aditya that is jquery. – Mr_Green Dec 24 '14 at 08:35
-
3this is such a simple web search `javascript page load`. Please show some efort before posting rudimantary questions here – charlietfl Dec 24 '14 at 08:35
-
yeah I mentioned that in the comment. @Mr_Green – Aditya Dec 24 '14 at 08:36
-
1I mean why he will use jquery to load a page? isn't that overkill? @Aditya – Mr_Green Dec 24 '14 at 08:37
-
@Mr_Green I thought he was asking how to execute a javascript code on page load and not load a page using javascript. And using jquery was just a suggestion for that. – Aditya Dec 24 '14 at 08:41
2 Answers
5
You can try
<!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>
Click here http://jsfiddle.net/NEfR2/

sagar43
- 3,341
- 3
- 29
- 49
0
document.ready()
is a jQuery event. JQuery’s document.ready()
method gets called as soon as the DOM is ready (which means that the browser has parsed the HTML and built the DOM tree). This allows you to run code as soon as the document is ready to be manipulated.
For example, if a browser supports the DOMContentLoaded event (as many non-IE browsers do), then it will fire on that event. However, IE can’t safely fire until the document’s readyState reaches “complete”, which is typically later.
You can find more info here (of course, this requires jQuery to be loaded)

facundofarias
- 2,973
- 28
- 27
-
1Ready is not a method of document - it is as you pointed out - a function of the jQuery library which accepts the document as an parameter. $(function() { // Handler for .ready() called. }); does exactly the same. – axel.michel Dec 24 '14 at 08:45