最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - js code is not working in an external file, but works file when placed in the same html page - Stack Overflow

programmeradmin4浏览0评论

I have this code in the footer of my html page

<script type="text/javascript">
// using jQuery
$('video,audio').mediaelementplayer();
</script>

the above code is adding video player on the html page.

Now I have created a separate js file in which I have already have some line of code which is creating owl sliders, tooltips, number counters etc.

When I add the above code into that separate js file it does not work, instead when I keep it in the footer of the html page it works fine.

I have this code in the footer of my html page

<script type="text/javascript">
// using jQuery
$('video,audio').mediaelementplayer();
</script>

the above code is adding video player on the html page.

Now I have created a separate js file in which I have already have some line of code which is creating owl sliders, tooltips, number counters etc.

When I add the above code into that separate js file it does not work, instead when I keep it in the footer of the html page it works fine.

Share Improve this question edited Jul 14, 2016 at 15:03 j08691 208k32 gold badges269 silver badges280 bronze badges asked Jul 14, 2016 at 14:58 Soeb SafiSoeb Safi 3413 silver badges17 bronze badges 2
  • If you place the reference to the external file in the head of the page you need to use a document.ready handler: ie $(function() { /* your code here... */ }); – Rory McCrossan Commented Jul 14, 2016 at 14:59
  • could you share us the full file? – Adam Commented Jul 14, 2016 at 15:08
Add a ment  | 

5 Answers 5

Reset to default 2

Try placing your code within $(function(){ ... }. This will execute when the DOM is loaded (currently your code is being executed before jQuery is loaded, if you check the JavaScript console, you will see an error something like $ is not defined)

 $(function(){
     $('video,audio').mediaelementplayer();
 });

or

 $( document ).ready(function() {
     $('video,audio').mediaelementplayer();
 });

You can read about what that is doing here. $(function() is the same as $( document ).ready()

your html(basically) should look like this:

<html>
  <head>
  </head>
  <body>
     <!-- html code here -->

     <!-- add jquery lib -->
     <script src="https://ajax.googleapis./ajax/libs/jquery/1.11.3/jquery.min.js"></script>
     <!-- your script -->
     <script src="you/file/path.js"></script>
  </body>
</html>

and your jquery file:

jQuery(function($) {
    // your functions here
    $('video,audio').mediaelementplayer();
});

Do you have a proper link to the separate js file in your page, generally at the bottom of the body? It should look something like this:

<script type="text/javascript" src="/joyride_odoo_models/static/js/scripts.js"/>

If you've done that properly, have you tried clearing your browser cache? You may need to do that to detect new javascript files.

How do you call your external js file ?
You must add your references js before your external js file.
you must add your function on document.ready.

You may wait until jQuery is full loaded or ready.

Ex.

$(document).ready(function($) {
    // Your code goes here
    $('video,audio').mediaelementplayer();
});

This code goes in external js file, then you need to include the file in the HTML

<script type="text/javascript" src="path/to/your/js/file"></script>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论