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!

Kunj
  • 1,980
  • 2
  • 22
  • 34
Sezzles
  • 111
  • 2
  • 10
  • 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? – fvgs Oct 28 '14 at 03:56
  • @Wolfram: JavaScript has automatic semicolon insertion; that won’t make a difference here. – icktoofay Oct 28 '14 at 03: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](http://www.sscce.org/)), we might be able to help you more definitively rather than offering shots in the dark. – icktoofay Oct 28 '14 at 04:00
  • @Wolfram: I don't know the appropriate code to call the functions inside the JQuery load script. – Sezzles Oct 28 '14 at 04:10
  • @inktoofay: The _googleWcmGet is working and the second function which is calling a google map. The google map isn't showing on any page. – Sezzles Oct 28 '14 at 04:11
  • @Sarr: Still, there’s a lot of things that could cause a Google Map to not show on a page. You’re going to have to dig into it a bit more to get more information about the root cause before we can help you figure out how to fix it. – icktoofay Oct 28 '14 at 04:20
  • Could you provide the code to these functions as well? What error message(s) (if any) is passed in the console? – Jason Oct 28 '14 at 04:28
  • 1
    Perhaps you need to wait until the map is loaded before you call `initialize()`? – Phrogz Oct 28 '14 at 04:36

5 Answers5

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(){ … });
Phrogz
  • 296,393
  • 112
  • 651
  • 745
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.

dashtinejad
  • 6,193
  • 4
  • 28
  • 44
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();
});
Sezzles
  • 111
  • 2
  • 10
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

chran
  • 119
  • 8
-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();
});
Kunj
  • 1,980
  • 2
  • 22
  • 34
  • 1
    I can’t imagine using any of these would solve any problems that would be encountered with the initial approach. – icktoofay Oct 28 '14 at 04:19
  • 1
    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. – Sezzles Oct 28 '14 at 04:45
  • You are welcome! $ and jQuery have same meaning :) http://stackoverflow.com/questions/3291680/jquery-syntax-when-to-use-dollar-vs-jquery – Kunj Oct 28 '14 at 05:29