Why wont this JS code work if its all on the same line?

Posted by culov on Stack Overflow See other posts from Stack Overflow or by culov
Published on 2010-03-16T07:57:57Z Indexed on 2010/03/16 8:06 UTC
Read the original article Hit count: 307

I'm writing HTML code for a java servlet. i first write the code in html/js so i can debug what im working on, and then ill make it a java string and put it in my servlet. My problem is that the code is working fine when i view it in ff from a local html file, but when i view it on my java servlet, it doesnt work because the js isnt getting called.

what I did was format the html that my servlet generated so that its not all on a single line and ran the code again. This time it worked. I copied this working code into a browser address bar so that it will all be on a single line, and copied that code back into the script in my html file. Now, when the previously working code is on a single line, it doesnt work.

Here's the formatted JS:

    var sMax
var holder;
var preSet;
var rated;

var request;

function rating(num){
    sMax = 0;
    for(n=0; n<num.parentNode.childNodes.length; n++){
        if(num.parentNode.childNodes[n].nodeName == "A"){
            sMax++;
        }
    }

    if(!rated){
        s = num.id.replace("_", '');
        a = 0;
        for(i=1; i<=sMax; i++){
            if(i<=s){
                document.getElementById("_"+i).className = "on";
                document.getElementById("rateStatus").innerHTML = num.title;
                holder = a+1;
                a++;
            }else{
                document.getElementById("_"+i).className = "";
            }
        }
    }
}

function off(me){
    if(!rated){
        if(!preSet){
            for(i=1; i<=sMax; i++){
                document.getElementById("_"+i).className = "";
                document.getElementById("rateStatus").innerHTML = me.parentNode.title;
            }
        }else{
            rating(preSet);
            document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
        }
    }
}


function rateIt(me){
    if(!rated){
        document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " "+me.title;
        preSet = me;
        rated=1;
        sendRate(me);
        rating(me);
    }
}


function sendRate(sel){

    alert("Your rating was: "+sel.title);

    addRating("rating", "?truck=kogibbq?rating="+ sel.id);
}



function addRating(servletName, servletArguments){
    var servlet = servletName;
    var arg = servletArguments
    var req = servlet + arg;
    alert(req);
    addrequest(req);
    request.onreadystatechange = function(){

        alert("response received");
    }
}

function addrequest(req) {
    try {
        request = new XMLHttpRequest();
    }catch (e) {

        try {
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (e) {
            alert("XMLHttpRequest error: " + e);
        }
    }

    request.open("GET", element, true);
    request.send(null);
    return request;
}

Thanks.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html