Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
Old 10-16-2011, 06:13 AM Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Hi Guys,
I have the following code.

I have a div named data that gets filled in by the data from data.php
How can i get it so it ONLY updates that div when the data is different as currently it doesnt.

It seems to update even though its the same ( If i have something selected in teh div, it will de-select when it updates ( although the html is the same ))

Thanks in advance.

Code:
<script language="javascript" type="text/javascript">
<!--
function getdata() {

$(document).ready(function(){
                       
    $.get("data.php", function(result){

    if(result !=  $("#data").html())
    {
        $("#data").html(result);
    }
    
  });
});

}
//-->

var timer = null;
timer = setInterval('getdata()', 2000);
</script>
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
 
Register now for full access!
Old 10-16-2011, 05:15 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Just a thought.
I assume the data that "data.php" is fetching is updated every now and then, probably stored in a database or something. It might be more efficient to set some flag or timestamp in the database to indicate weather or not the data has been changed since it was last requested, directly in data.php, and respond with a status variable.

That way, the data would not need to be sent every time your javascript timer ticks. You only need to send a boolean value basically (0 or 1), to tell if there was an update or not. And if there was an update, of course you send the data as well.

This would let less bandwidth go to waste and be slightly faster (depending on how much data you're fetching) and solve your initial issue
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-16-2011, 05:31 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Just tried that by making data.php fully static.
Ie: it returns Hello and nothing else.

The page with the JS still updates even though the html is the same?

Is my JS completely flawed or am I going to have to debug further?
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-16-2011, 05:40 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Hmm, well, it looks right to me.
Try showing the content in an alert
Code:
$(document).ready(function(){
                       
    $.get("data.php", function(result){

    alert("Old value: " + $("#data").html() + "\nNew value: " + result);

    if(result !=  $("#data").html())
    {
        $("#data").html(result);
    }
    
  });
});
Could you provide a link to your site?
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-16-2011, 05:48 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
It may be easier to take a hash of each value rather than trying to eyeball it. There could be some whitespace throwing off the comprison.

http://pajhome.org.uk/crypt/md5/md5.html
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-16-2011, 05:53 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Quote:
Originally Posted by lizciz View Post
Hmm, well, it looks right to me.
Try showing the content in an alert
Code:
$(document).ready(function(){
                       
    $.get("data.php", function(result){

    alert("Old value: " + $("#data").html() + "\nNew value: " + result);

    if(result !=  $("#data").html())
    {
        $("#data").html(result);
    }
    
  });
});
Could you provide a link to your site?
Hmmm..
Just alerted if the data is different and sure enough JS thinks it is..
I have a feeling it may be related to some kind of HTMl encoding..
IE: it gets sent encoded, JS decodes and sees it different each time..

*maybe*

LOL

Will get it to output what it *thinks* is there and what its replacing with..

Maybe that will help me on my trail.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-16-2011, 06:04 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Is there another way to get the HTML / the content of a DIV thats been dynamiccly injected with AJAX?

It appears that the script isnt able to get anything back from the div..
IE: It always thinks its empty even though it has content thats been injected using ajax..

ive tried document.getelementbyid(div).innerhtml..
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-16-2011, 06:19 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Ithink what ill do is put the data into a new var and use that to compare against..
Just need to figure out how to do that correctly now lol
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-16-2011, 06:25 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Whoooohoooo

Done it after 24 hours lol..

Rather than pull data from the div, ill just save it to its own dedicated var now..

Code:
<script language="javascript" type="text/javascript">
<!--
// Declare the olddate var so script knows its different
var olddata = "Empty";

// Get data function..
function getdata() {

$(document).ready(function(){
    // Get data from streamer                       
    $.get("data.php", function(result){
    
    // If var result != #data (the div) then update the div.( Ie, if the data is different to teh div, update the div. )

  //  document.write('Old Data<br>');
  //  document.write(olddata);
  //  document.write('New Data<br>');
  //  document.write(result);
    
    
    if(result !=  olddata)
    {
        olddata = result;
    //    alert('Data different' + result);
        $("#data").html(result);
    }
    
  });
});

}
//-->

var timer = null;
timer = setInterval('getdata()', 3000);
</script>
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-16-2011, 06:28 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
GFRRRRRRRRRRRRRR

Maybe not.

Appears that Firefox doesnt like this method.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-16-2011, 06:32 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
K,
Above method works in:
Chrome - YES
FF - NO
IE - NO.

I really dont understand why you need to write 10 different methods just to get what is supposedly a standard to work!
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-16-2011, 06:36 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Maybe you should try handling the issue server side. Take a hash of the div content before displaying it then every time you request new data send that hash as a get param. Before sending the new content, compare the hashes. If they're the same don't output anything.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-16-2011, 06:40 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Quote:
Originally Posted by NullPointer View Post
Maybe you should try handling the issue server side. Take a hash of the div content before displaying it then every time you request new data send that hash as a get param. Before sending the new content, compare the hashes. If they're the same don't output anything.
I really dont think that will work as the problem is still client side.
The JS just refuses to run on FF and IE but works perfectly on Chrome.

Ive manually added values in and the JS works perfect again on Chrome.
Just FF and IE refuse to play ball. ( They run the function once and never again.. )
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-16-2011, 06:44 PM re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
OMG!!!
I found the issue!

It was my debugging breaking it!

the document.write ( or document.writeln ) was replacing the WHOLE HTML in IE and FF so the JS didnt run again ( because it wasnt there )

Yet chrome appended the JS document.write ( I think this is how its meant to be handled ) so the JS then ran again ( because it was there!!!!!!!!!!!!! )
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-17-2011, 05:46 AM Re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You should have stuck to the alert() :P
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-19-2011, 01:37 PM Re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
Extreme Talker

Posts: 246
Trades: 0
Just out of curiosity, why not use the load method?

Code:
<script language="javascript" type="text/javascript">
$(function(){
    // Get data function..
    function getdata() {
        $('#data').load('data.php');
    }

    var timer = setInterval('getdata()', 3000);
});
</script>
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 10-19-2011, 02:38 PM Re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Because i couldnt figure out how to .load into a var.

The problem I had with .load was that i DIDNT! want it to load into the DIV if the DIV already contained the same data.

( .load )
Loaded straight into a div.

( .get )
Loaded into a var,
Checked if its the same or not.
If its different it would then populate the div.

It may be that .load can load into a var the same as .get ?
If so, I still cant see any reason for one over the other in this case?
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 10-19-2011 at 02:41 PM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-19-2011, 03:36 PM Re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
Extreme Talker

Posts: 246
Trades: 0
In your example, you are still getting all of the data every time, whether it is the same or not, you just aren't loading it into the div. Loading it into the div every time only becomes an issue if you have an extreme amount of information to load into it.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 10-19-2011, 03:44 PM Re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Quote:
Originally Posted by stbuchok View Post
In your example, you are still getting all of the data every time, whether it is the same or not, you just aren't loading it into the div. Loading it into the div every time only becomes an issue if you have an extreme amount of information to load into it.
Yep , it gets the data every time.
The problem isnt the amount of data, Its browser flicker and focus loss.

Ie:
when it updates a div, If some text is highlighted then the highlight gets lost every time the div updates ( even if nothing is worth updating )

Also, on slower browsers or slower computers you can get browser flicker where the div is blatently refreshing rather than instantly on most normal computers today.

The data being retrieved is tiny.
It was to simply fix the above issues ( mainly the loss of focus issue when the html in a div gets updated. )
Its pointless forcing a user to loose focus if theres nothing worth updating.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 10-19-2011 at 03:46 PM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-20-2011, 04:22 AM Re: Problem with jquery .get ( Only update div when changed ) - [ Fixed ]
Novice Talker

Posts: 10
Name: Umar Bukhari
Location: Pakistan
Trades: 0
The simple and easiest way to this is to save the data into database and show the data from database to the dive where you want it to show.

one function will get data from "data.php" via ajax and save the data into database and it will also check if the data is already in database or not. If it is in database the script will skip that and move to next line.

And the other function will get data from database using ajax it will simply get data and no need to check if something is already exist or not.

the 2nd function will be triggered right after the first function. So it will do the same thing that you are looking for.
angelsdev is offline
Reply With Quote
View Public Profile Visit angelsdev's homepage!
 
Reply     « Reply to Problem with jquery .get ( Only update div when changed ) - [ Fixed ]

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55420 seconds with 12 queries