1

Well it looks like how I tried to explain before wasn't working as well. I'll just explain my full purpose, show my code and what I'm doing. I am writing a Google Chrome extension which is mainly run with jQuery. The purpose of the extension is to provide a few css changes, script changes, and site changes to a website I am a member of. I do not have administrative access to the site so I do not have access to change the raw files and simply add an "onLoad='create_toolbar()'" attribute to the body. When the body/page/document loads, I want to run a function which is in the site's Javascript called "create_toolbar". I need to find a way to call the function of create_toolbar when the page is loaded but still run the jQuery in my extension which cleans up some bugs on the site.

Is there a way for me to call the create_toolbar function when the document is ready with jQuery? Can I append an attribute to an empty div for when onload it calls the create_toolbar function?

I've tried the following and none have worked.

$(document).ready(create_toolbar);

.

$(document).ready(function(){
$(body).attr('onLoad', 'create_toolbar()');
};

Never mind guys, I used the following code below to solve my problem:

$(document).ready(function(){
$('body').attr('onMouseOver', 'create_toolbar()');
});
8
  • Please show more of your code. Commented Aug 11, 2011 at 16:56
  • That will call your function when the DOM is ready. There isn't enough information in the question to really understand the issue you're having. Commented Aug 11, 2011 at 16:56
  • 3
    This should work just fine jsfiddle.net/KtCXM Commented Aug 11, 2011 at 16:57
  • 3
    BTW, jQuery is just a set of Javascript functions. Your code "in jQuery" is Javascript code. There is thus no such concept as "inside of jQuery". Commented Aug 11, 2011 at 17:13
  • 1
    To answer your question, we need to know how create_toolbar is defined. Also it would be helpful to know what the error message is instead of just "none have worked" Commented Aug 11, 2011 at 17:27

2 Answers 2

4

To expand on @Gabriel's answer, in the following cases the OP's original code should work as is:

function create_toolbar() {
    alert("ran it");   
};
$(document).ready(create_toolbar);

.

$(document).ready(create_toolbar);
function create_toolbar() {
    alert("ran it");   
};

.

var create_toolbar = function(){
    alert("ran it");   
};
$(document).ready(create_toolbar);

In this case, the OP's code would not work, but @Gabriel's solution would:

//Doesn't work
$(document).ready(create_toolbar);
var create_toolbar = function(){
    alert("ran it");   
};

.

//Works
$(document).ready(function() { create_toolbar(); });
var create_toolbar = function(){
    alert("ran it");   
};

The reason is that in the OP's code, create_toolbar needs to exist at the time the line $(document).ready(create_toolbar); is executed. In @Gabriel's solution with a closure, create_toolbar doesn't need to exist until the document is ready.

Sign up to request clarification or add additional context in comments.

Comments

3

Closures:

$(document).ready(function() { create_toolbar(); });

4 Comments

Could you explain why OP's original call wouldn't work? This looks like it should be functionally equivalent, but I'm probably missing something.
@Davy: This will help if create_toolbar is a local variable whose value is assigned later.
@Gabriel It still isn't working :/ I'll update my post now with my full code and my purpose of doing this.
@Gabriel I hope you didn't mind me expanding on your answer with my own. It's too long for a comment, and I had to play around with different examples to really understand the difference.

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.