简体   繁体   中英

Calling document.ready function of file from another

I have two html files. One file has a table which is constantly updated by the script written in the same. The table part is in body of the html and its contents are updated once the document.ready is executed. I want to run the document.ready of file from the another file and show the table(which is constantly updating and present in another file) in that file.

FILE -1

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

      $('#maindiv').load('http://somesite/index.html #tableId');


       });
 </script>
<body>
<div id="maindiv"></div>
</body>

FILE- 2(index.html page)

<script>

      $(document).ready(function(){

          $.ajaxSetup({ cache: false });
          some_function(true);
          setInterval(function(){some_function(false)},500);
      });

      //Some Global Variables Declared

var some_function= function(getAll){
//Accessing and using the above variables
}
</script>
<body>
<div id="tableId">
<table>
 ......
</table>
</div>
</body>

If I got your right. You should do global functions . Lets say you have the scenario below.

File one

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

function doSomething() {
    //doing something
}

File two

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



EDIT
If you want to get html from another page you can use the jQuery .load() . You would have to have the javascript that makes it dynamic on the file you "import" it to.

file one html

<div class="load-table-here"></div>

file two html

<table class="you-want-me"></table>

you load it like this

UPDATE

$('.load-table-here').load('path/file-two.html .you-want-me', function() {
      some_fuction(true);
      setInterval(function(){some_function(false)},500);
});

In the jQuery.load documentation, look at the "Script Execution" section: http://api.jquery.com/load/ . The important example to point out here is,

However, in the following case, script blocks in the document being loaded into #b are stripped out and not executed:

 $( "#b" ).load( "article.html #target" ); 

So, in other words, the scripts loaded for your other file are never executed and can never update your new table.

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