Janaaaa
Janaaaa

Reputation: 1406

putting function in a separate js file

I have the following in my main html:

jQuery(document).ready(function(){
jQuery('body').flvplay(songlist, {something});});

I wanted to move this to a separate JS file and make a function call from the main html. I created the new JS with the following:

play()
{
jQuery('body').flvplay(songlist, {something});
}

Finally, I called the play function from the main html after including the new JS file:

<script type="text/javascript">
play();
</script>

This is not working for some reason. Is there anything wrong with the above? Thanks for helping.

Upvotes: 1

Views: 1329

Answers (2)

icktoofay
icktoofay

Reputation: 128991

You need to declare functions using the function keyword:

function play()
{
    jQuery('body').flvplay(songlist, {something});
}

Additionally, you deleted $(document).ready(...), which is important because otherwise you might be trying to manipulate elements that do not exist because they have not loaded yet. You may want to modify your calling code to look like this:

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

If you don't use the function keyword, the code in the referenced file will be parsed like this:

play()  // call the function `play` (which has not yet been defined)
        // as it does not yet exist, an error is thrown and execution is halted
{  // open a block (which is not useful here,
   //               since JavaScript has no block scope)
     jQuery('body').flvplay(songlist, {something});  // do something with body
}

Upvotes: 3

codingbiz
codingbiz

Reputation: 26386

There is no function keyword before play()

function play()
{
    jQuery('body').flvplay(songlist, {something});
}

Upvotes: 0

Related Questions