简体   繁体   中英

Calling external function from jquery (document).ready?

I'm trying to call a function in an external file from within a HTML page using the document.ready jquery functionality. Below is the example of code from my HTML, but it does not execute the function with the code I've written.

<script type="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="_js/script.js">
     //<![CDATA[
     $(document).ready(function(){
     // What do I run here to grab external file function?
         extFunction();
     });
     //]]/>

Example of function from external file:

function extFunction(){
    alert("ALERTED!");
};

<script type="text/javascript" src="_js/script.js">

You cannot have a src attribute on a <script> tag and also have JavaScript code inside the tag. Once the src attribute is seen by the browser, it does not execute anything within the tag. Please make two separate tags...

<script type="text/javascript" src="_js/script.js"></script>

<script type="text/javascript">
//<![CDATA[
     $(document).ready(function(){
     // What do I run here to grab external file function?
         extFunction();
     });
     //]]/>
</script>

You cannot have body(content) and src for a script element

<script type="text/javascript" src="_js/script.js"></script>
<script type="text/javascript">
     //<![CDATA[
     $(document).ready(function(){
     // What do I run here to grab external file function?
         extFunction();
     });
     //]]/>
</script>

you want to call function from a php file or js file ?? if you want to call a function of another .js file just include that file in your .js file and if you want to call a php function then use ajax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM