1

I want to call a function from java-script file in my html page. My code is shown below.

index.html

<!DOCTYPE html>
<html><body>
<h2>Web1</h2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="js/main.js"></script>
<script type="text/javascript">
    $(function() {
        myFunction1();        // call function
});
</script>

<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="socket.io.js"></script>
</body>
</html>

main.js

$(function() {
function myFunction1() {
    alert("ff");
}

myFunction1() is my function. It is inside $(function() {}. But it is not working. It is working when wrote in main.js

function myFunction1() {  // function declaration in global scope
alert("ff");
}

How to define myFunction1() inside $(function() {} in main.js? Please help me?

1
  • I have clear it. but my problem is not solve. Commented Apr 25, 2017 at 6:23

6 Answers 6

2

Try this:

(function() {
  function myFunction1() {
      alert("ff");
  }
})()

Notice the syntax errors in the last line })().

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

3 Comments

Try it with this: <script type="text/javascript"> $(document).ready(function(){ myFunction1(); }); </script>
The console should be in the browser you're running your html/javascript in. Press F12 and a panel should open up. Check in the Console tab
I want to call the function inside $(function() {} in js file
0

In my opinion it doesn't look like it really need to be nested inside the anonymous function. If you remove it it should just fine.

Comments

0

Try to call your function myFunction1() inside .ready() event

It will execute when the DOM is fully loaded.

<script type="text/javascript">
   $(document).ready(function(){    
        myFunction1(); 
   });

</script>

Comments

0

<!DOCTYPE html>
<html>
<head>
<!--
You can uncomment this in your case
<script type="text/javascript" src="js/main.js"></script>
-->
<script   src="https://code.jquery.com/jquery-3.2.1.min.js"   integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="   crossorigin="anonymous"></script>
</head>

<body>
<h2>Web1</h2>

<script>
$(document).ready(function(){
myFunction1();
});
function myFunction1() {
    alert("ff");
}
</script>
</body>
</html>

Here is your answer. Let me inform if it is helpful to you or not. Thank you!

2 Comments

I want to write the js function in js file.
Yeah! you can do it by just copy paste the script portion into your separate js file and then do uncomment my commented code of script tag. :)
0

You are missing import jQuery and syntax errors in the last line })()

Try This :

<!DOCTYPE html>
<html><body>
<h2>Web1</h2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script  type="text/javascript" src="js/main.js"></script>
<script type="text/javascript">
    $(document).ready(function(){    
        myFunction1(); 
   });
function myFunction1() {
  alert("ff");
}
</script>
</body>
</html>

OR

<!DOCTYPE html>
<html><body>
<h2>Web1</h2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script  type="text/javascript" src="js/main.js"></script>
<script type="text/javascript">
    $(function(){    
        myFunction1(); 
   });
function myFunction1() {
   alert("ff");
}
</script>
</body>
</html>

Comments

0

You need jquery for using parts of it later.

Basically you need to switch function declaration and function call, because inside of jquery's onload, you need the call of the function.

Puth the function declaration outside in the global scope.

function myFunction1() {  // function declaration in global scope
    alert("ff");
}

$(function() {
    myFunction1();        // call function
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Web1</h2>

Version with local function declaration.

$(function() {
    function myFunction1() {  // local function declaration
        alert("ff");
    }

    myFunction1();            // call function
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Web1</h2>

6 Comments

It's working. but I want to call the function inside $(function() {} in js file.
you call it inside, but maybe i don't understand you problem. please add some more information.
In my html iwant to call myfunction1(). The function is defined in js file and it inside $(function() {}. So i want to call the function from $(function() {}. Now it is not working.
then take the first proposal with a global function.
how is it? can you please explain?
|

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.