mindSmile
05-26-2010, 10:00 AM
Hey there, I'm trying to get this script working and having a problem with it in firefox. Essentially, I want to select a bit of text, and as soon as I release the mouse on the selection, I want that selected text to be passed in to my XMLhttprequest (which is happening perfectly) and for the text to become deselected. This is working fine in Chrome and Safari, but not in Firefox. Firebug is telling me "document.getSelection().removeAllRanges is not a function"
The removeAllRanges is part of the emptySelection() function. Sorry for including so much code. I'm not well-seasoned in javascript and I really don't know which part may be the culprit.
Does anyone know why firefox isn't recognizing removeAllRanges as a function or how I can get around it to make the deselection happen right away?
var text = "";
var time_variable;
function getXMLObject(){
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
var xmlhttp = new getXMLObject();
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("st_translatebox").innerHTML=xmlhttp.responseText+" <a href=\"\" id=\"close\">X</a>";
document.getElementById("close").onclick = st_hideTranslateBox;
}
}
}
function ajaxPost(){
var getdate = new Date();
if(xmlhttp) {
xmlhttp.open("POST","translate.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send();
}
}
function getCookie(c_name){
if(document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1){
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if(c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +encodeURI(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}
function st_initCapture(){
var IE = /*@cc_on!@*/false;
if(!IE){
window.captureEvents(Event.CLICK);
document.onmouseup = captureTranslate;
}
else{
document.onmouseup = captureTranslate;
}
}
function getSelection() {
if(document.getSelection){
return document.getSelection();
}
else if(document.selection){
return document.selection.createRange().text;
}
else{
return window.getSelection();
}
}
function emptySelection() {
if(document.getSelection){
return document.getSelection().removeAllRanges();
}
else if(document.selection){
return document.selection.blur();
}
else{
return window.getSelection().removeAllRanges();
}
}
function openTranslate(e){
var st_translatebox = document.getElementById("st_translatebox");
if(st_translatebox){
document.body.removeChild(st_translatebox);
}
var element = document.createElement("div");
var IE = /*@cc_on!@*/false;
if(IE){
var posx = event.x;
var posy = event.y;
}
else{
var posx = e.pageX;
var posy = e.pageY;
}
element.style.position = "absolute";
element.style.left = posx+"px";
element.style.top = posy+6+"px";
element.setAttribute("id","st_translatebox");
document.body.appendChild(element);
if (document.getElementById("close")){document.getElementById("close").onclick = st_hideTranslateBox;}
}
function captureTranslate(e){
text = getSelection();
if(getSelection() != "" && getSelection() != null){
setCookie("st_query",text,1);
ajaxPost();
openTranslate(e);
emptySelection();
}
}
st_initCapture();
Thanks!
The removeAllRanges is part of the emptySelection() function. Sorry for including so much code. I'm not well-seasoned in javascript and I really don't know which part may be the culprit.
Does anyone know why firefox isn't recognizing removeAllRanges as a function or how I can get around it to make the deselection happen right away?
var text = "";
var time_variable;
function getXMLObject(){
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
var xmlhttp = new getXMLObject();
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("st_translatebox").innerHTML=xmlhttp.responseText+" <a href=\"\" id=\"close\">X</a>";
document.getElementById("close").onclick = st_hideTranslateBox;
}
}
}
function ajaxPost(){
var getdate = new Date();
if(xmlhttp) {
xmlhttp.open("POST","translate.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send();
}
}
function getCookie(c_name){
if(document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1){
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if(c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +encodeURI(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}
function st_initCapture(){
var IE = /*@cc_on!@*/false;
if(!IE){
window.captureEvents(Event.CLICK);
document.onmouseup = captureTranslate;
}
else{
document.onmouseup = captureTranslate;
}
}
function getSelection() {
if(document.getSelection){
return document.getSelection();
}
else if(document.selection){
return document.selection.createRange().text;
}
else{
return window.getSelection();
}
}
function emptySelection() {
if(document.getSelection){
return document.getSelection().removeAllRanges();
}
else if(document.selection){
return document.selection.blur();
}
else{
return window.getSelection().removeAllRanges();
}
}
function openTranslate(e){
var st_translatebox = document.getElementById("st_translatebox");
if(st_translatebox){
document.body.removeChild(st_translatebox);
}
var element = document.createElement("div");
var IE = /*@cc_on!@*/false;
if(IE){
var posx = event.x;
var posy = event.y;
}
else{
var posx = e.pageX;
var posy = e.pageY;
}
element.style.position = "absolute";
element.style.left = posx+"px";
element.style.top = posy+6+"px";
element.setAttribute("id","st_translatebox");
document.body.appendChild(element);
if (document.getElementById("close")){document.getElementById("close").onclick = st_hideTranslateBox;}
}
function captureTranslate(e){
text = getSelection();
if(getSelection() != "" && getSelection() != null){
setCookie("st_query",text,1);
ajaxPost();
openTranslate(e);
emptySelection();
}
}
st_initCapture();
Thanks!