JavaScript does not load when opening my web page.

Hello, I try to run this code, but every time I open my web page, it does not load, and I do not get the text that I echo with php in the file "money.php". But if I open the console in my browser and run the code that it will download. Does anyone know how I can fix this?

<script type="text/javascript"> $('[data-update]').each(function() { var self = $(this); var target = self.data('update'); var refreshId = setInterval(function() { self.load(target); }, self.data('refresh-interval')); }); </script> 
 <div data-update="money.php" data-refresh-interval="500"> 
+5
source share
3 answers

Use it

  <script type="text/javascript"> $(function() { $('[data-update]').each(function () { var self = $(this); var target = self.data('update'); var refreshId = setInterval(function () { self.load(target); }, self.data('refresh-interval')); }); }); </script> 

Or that

  <script type="text/javascript"> $(document).ready(function() { $('[data-update]').each(function () { var self = $(this); var target = self.data('update'); var refreshId = setInterval(function () { self.load(target); }, self.data('refresh-interval')); }); }); </script> 
+2
source

Wrap your code in $(function(){ ... }); so that it starts after the DOM is ready:

 <script type="text/javascript"> $(function(){ $('[data-update]').each(function() { var self = $(this); var target = self.data('update'); var refreshId = setInterval(function() { self.load(target); }, self.data('refresh-interval')); }); }); </script> 

Additional information: http://api.jquery.com/ready/

+1
source

Run javascript / jquery after

 <div data-update="money.php" data-refresh-interval="500"> 
+1
source

All Articles