I am trying to add multiple onload functions into my <body>
My current code:
<body onload="_googWcmGet(number, '1800 000 000'); initialize()">
The _googWcmGet is working but the second function isn't working... Please help!
I am trying to add multiple onload functions into my <body>
My current code:
<body onload="_googWcmGet(number, '1800 000 000'); initialize()">
The _googWcmGet is working but the second function isn't working... Please help!
document.body.addEventListener( 'load', function1, false );
document.body.addEventListener( 'load', function2, false );
// etc.
Or, if you're using jQuery, just use as many of these as you need:
$(function(){ … });
$(function(){ … });
$(function(){ … });
There is no different how many statements you wrote in onload
event:
function f() {
console.log('f');
}
function g() {
console.log('g');
}
<body onload="f(); g()"></body>
I believe you have an error in your first function:
function f() {
console.log(undefinedVariable);
}
function g() {
console.log('g');
}
<body onload="f(); g()"></body>
As you see, the g()
won't execute as there is an error in the first function.
I modified the code provided above with a JQuery instead of a $. This code is now working well.
Correct Code - Functioning Well:
jQuery(document).ready(function() {
_googWcmGet('number', '1800 198 885');
initialize();
});
It seems that console.log is the problem. Take a look at this link https://developer.mozilla.org/en-US/docs/Web/API/Console.log
Calling multiple functions onload is possible like you did, see article. Here are other ways:
I
function init() {
_googWcmGet(number, '1800 000 000');
initialize();
}
<body onload="init()">
//or
window.onload = init;
II
$(document).ready(function() {
//or
//$(function() {
_googWcmGet(number, '1800 000 000');
initialize();
});