0

I am using a jQuery call to grab content from one page (contentPage) and re-display it every 30 seconds to the current page (tablePage) I am on. This works fine. When the number of items changes on contentPage, I want to refresh tablePage so it will rebuild the table entirely, rather than just updating the table values themselves.

Is there a way to send a refresh from contentPage to tablePage?

Note: I want to refrain from using some sort of count variable on contentPage as the deciding factor on tablePage if possible.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
user2537383
  • 315
  • 8
  • 19
  • 1
    How are `tablePage` and `contentPage` related? – gen_Eric Aug 13 '13 at 21:24
  • Could you just put a JS function inside `tablePage` and have `contentPage` call it? – gen_Eric Aug 13 '13 at 21:25
  • That just might work. On **tableBage** I can have a `funtion refreshPage(){location.reload();}`. Then have **contentPage** call it. BUT, wouldn't that still refresh **contentPage**? Wouldn't I just be using a function from **tablePage** and executing it on **contentPage**? – user2537383 Aug 13 '13 at 21:29
  • 1
    If I get you right and you send an ajax request from `tablePage` to `contentPage` then you could just return some flag that the page has to be refreshed and check for that in the callback function(in `tablePage`) of your request. – Marcel Gwerder Aug 13 '13 at 21:32
  • If you wan't to "*push*" a refresh from `contentPage` to `tablePage` you would have to use some realtime client server communication for example with [socket.io](http://socket.io/). – Marcel Gwerder Aug 13 '13 at 21:45
  • I have decided to use a boolean to determine whether to refresh or not. Can you look at my below comment and see if you have a solution? – user2537383 Aug 13 '13 at 21:53

2 Answers2

0

Just include some data from the contentPage like refresh=true and check that variable on the tablePage and if it is 'true' execute the following javascript:

window.location.href = '{MyUrl}' + '?nocache=' + Date.parse(new Date());

The nocache is a helpful to ensure a refresh depending on the browser.

Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • That is what I did not want to do, but I think I may have to. Quick question, is there a way to FORCE a refresh? Because I am using some post variable that I need. And my browser always gives me: `To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.` Or maybe post the same variable to the same page to refresh correctly? – user2537383 Aug 13 '13 at 21:41
  • Maybe these questions are useful: http://stackoverflow.com/q/6661175/983992 and http://stackoverflow.com/q/4869721/983992 – Marcel Gwerder Aug 13 '13 at 22:03
  • I tried using `window.location.href = window.location.href;` but I lose all my POST data. Still looking for a way to keep the post data – user2537383 Aug 13 '13 at 22:14
  • You will have to rebuild the url and if you can include your data in the query string you are all set, if you post data is too complex than you cannot do this. I have another suggestion, will post now. – Brian Ogden Aug 13 '13 at 22:27
  • Brian, I only have one POST variable. Pretty much the one that determines whether or not to build the table. I did a little work around for now. In my **contentPage** I set a SESSION variable if the the number of items has changed since last time. Then on my **tablePage** I reload the page with `window.location.href = window.location.href;` (which unsets all POSTS) and load the table if it is set, and then unset the SESSION immediately after the table is rebuilt, thus allowing me to still load the page properly. But it is sloppy and I would like to find a way around this. – user2537383 Aug 13 '13 at 22:34
0

So another direction maybe you can take is create a template of your table in javascript and rebuild every time in a function. I am thinking the following code will help you rethink your current direction a bit so you don't have to refresh that page, you just refresh the table and you can use current session trigger to call function buildTable instead of location.href = location.href, just a guess, since you didn't include any code snippets: (I am using jQuery at certain points in this javascript)

function buildTable(postDataFromContentPage){
     var trItems = [];
     var itemHeaderTemplate = '<th>{DataItemTitle1}</th><th>{DataItemTitle2}</th>';
     var itemBodyTemplate = '<tr><td>{DataItem1}</td><td>{DataItem2}</td></tr>';

     itemHeaderTemplate = itemHeaderTemplate.replace('{DataItemTitle1}',data.DataItemTitle1).replace('{DataItemTitle2}',data.DataItemTitle2);

     $('#tableHeader').html(itemHeaderTemplate);

     $.each(data.RowItemsArray, function (index, element) {
           //RowItemsArray is a 2 dimensional array
            itemBodyTemplate = itemBodyTemplate.replace('{DataItem1}',element{0}).replace('{DataItem2}',element{1});
            trItems.push(itemBodyTemplate );
        });

      $('#tableBody').html(items.join(''));

      //now you can just call buildTable function anytime to rebuild table
}//end function

<!--HTML-->
    <table>
    <thead id="tableHeader"></thead>
    <tbbody id="tableBody"></tbody>
    </table>
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • I appreciate the response, although I don't think this will simplify things for me. In my contentPage I set a SESSION variable if the the number of items has changed since last time. Then on my tablePage I reload the page with `window.location.href = window.location.href;` (which unsets all POSTS) and load the table if it is set, and then unset the SESSION immediately after the table is rebuilt, thus allowing me to still load the page properly. It works fine and doesn't force me into an infinite table loading loop or fail to load it. I'll probably just stick with this for now. – user2537383 Aug 13 '13 at 22:56
  • Why not just have some ajax in your table page that checks for reload on a timer. Just ping your server for the current session state every 5 seconds and then reload the table if needed. I think part of the problem is that you should have problem included some code of your current setup – Brian Ogden Aug 13 '13 at 22:58