views:

123

answers:

2

I have a script and when it detects a difference it plays a little alert sound. When it alerts the sound plays but loops, how can I stop this?

A: 

How are you doing this? You're not still using the code people used in the early nineties to embed MIDI files into web pages, are you?

<embed src="file.wav" autostart="true" loop="false" volume="100" hidden="true" />
amphetamachine
I have found the problem. It compares old HTML to the new response. if they are different they sound. For some reason it thinks they're different everytime request comes through even though they're not :/
Eme
A: 
if($("box").html() != html)

Getting the html() (innerHTML) from a node won't return the same markup as was originally parsed to form the contents of the node. The browser can output attributes in any order, with any type of quoting style, change whitespace, change case, choose to &-escape differently, etc. You will have to keep the old HTML return value in a variable to compare against.

bobince
Thats what I did, doesn't seem to be working though :S
Eme
var oldHTML = $("#chatbox").html(); $.ajax({ url: "logs/log.html", cache: false, success: function(html){ if(oldHTML != html){ $("#soundContainer").html('<embed src="alert.mp3" autostart="true" loop="false" volume="100" hidden="true" />'); }
Eme
You're still getting `oldHTML` from `.html()`! You need to store the previous value of the `html` argument to compare against. Serializing document content with `html()` will not give you the same string you put in.
bobince
Yeh but the oldHTML is before the AJAX request?
Eme
Then you will have to have the original HTML in a JavaScript string variable, or another mechanism to detect changes such as a last-modified-time. You *cannot* get back the original markup of a page from `innerHTML`/`html()`; once it has been parsed it does not exist any more. Page content at run-time is stored as `Element`, `Attribute`, `Text` etc. Node objects, which when serialised back to HTML will almost certainly result in different markup to that from which it was parsed.
bobince