2

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!

8
  • Try ending the initialize() with a ;. The better solution would be to use jQuery. Seeing as you tagged the post jQuery, have you not tried simply calling the functions inside a jQuery load script? Commented Oct 28, 2014 at 3:56
  • @Wolfram: JavaScript has automatic semicolon insertion; that won’t make a difference here. Commented Oct 28, 2014 at 3:57
  • 1
    We’re going to need more information than “isn’t working” to be able to help you. Check in your browser’s developer tools console and see if any errors appear there. If not, try using the debugger and seeing where something you don’t expect happens. Once you have a concrete error (and preferably an SSCCE), we might be able to help you more definitively rather than offering shots in the dark. Commented Oct 28, 2014 at 4:00
  • @Wolfram: I don't know the appropriate code to call the functions inside the JQuery load script. Commented Oct 28, 2014 at 4:10
  • 1
    Perhaps you need to wait until the map is loaded before you call initialize()? Commented Oct 28, 2014 at 4:36

5 Answers 5

1
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(){ … });
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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();
});

Comments

0

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

Comments

-1

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();
});

3 Comments

I can’t imagine using any of these would solve any problems that would be encountered with the initial approach.
I used the $(document).ready one placing the initialize within there. I got the code working by changing the $ to JQuery. Seems to be working now. Thanks.
You are welcome! $ and jQuery have same meaning :) stackoverflow.com/questions/3291680/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.