问题
It's a followup question of this. I've serialized the selection of text in an iframe and want to restore it but it gives me an error, "Uncaught Error: deserializeRange: checksums of serialized range root node (XXXXX) and target root node (XXXXX) do not match"
var iframe = $("iframe")[0];
var selection = rangy.getSelection(iframe);
var serializeSelection = rangy.serializeSelection(selection);
alert(serializeSelection);
rangy.deserializeSelection(serializeSelection);
Demo: http://jsfiddle.net/codef0rmer/2d6Bk/1/
回答1:
You need to provide the window object as the third parameter:
rangy.deserializeSelection(serializeSelection, null, iframe.contentWindow);
Demo: http://jsfiddle.net/2d6Bk/2/
This is more awkward than it should be. Version 1.3 allows you to use iframe
rather than iframe.contentWindow
, but it's still too unreadable so I will improve this somehow.
回答2:
Regarding "Uncaught Error: deserializeRange: checksums of serialized range root node (XXXXX) and target root node (XXXXX) do not match" error:
Rangy serialization and de-serialization should be done to same format and type text. Sometimes the text or format or css may be changes while deserializing. So you have to take care of your text. As your using Iframe, call the methods once after your text has been loaded in the iframe. The mistake what we do is we serialize the text after page loads and while deserializing, we try to deserialize the text after iframe loading. Just check whether your iframe is loaded with content or not before you start deserializing.
来源:https://stackoverflow.com/questions/11586115/range-deserializeselection-checksum-error