views:

85

answers:

3

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.

+4  A: 

You are missing semi-colons on:

var sMax

and

var arg = servletArguments
Sarfraz
It's missing semi-colons in many places, every statement should end with a semi-colon, specially if running it through a minifier. PhpStorm can warn you about missing semi-colons, or just use JSLint as others mentioned.
Juan Mendes
A: 

There's a ; missing in the first line of your javascript file.

Darin Dimitrov
+2  A: 

Sarfraz has already pointed out the deal-breakers in this code, but for similar problems in the future, I recommend pasting your code into JSLint, for validation.

It'll find a lot of errors that won't actually break your code (but then neither will the lack of semicolons unless you put the entire script in one line), so you don't need to fix every single remark just to get it working, but of course, if you can follow the exact JSLint recommendations, that's usually just great.

David Hedlund