当前位置: 动力学知识库 > 问答 > 编程问答 >

xmlhttprequest - Javascript Syntax Error

问题描述:

Can anyone figure out why this is throwing a syntax error? All of the code looks correct to me.

<script type="text/javascript">

var rootdomain="http://"+window.location.hostname;

function ajaxinclude(url)

{

var pagerequest = false;

if (window.XMLHttpRequest) // if Mozilla, Safari etc

pagerequest = new XMLHttpRequest()

else if (window.ActiveXObject){ // if IE

try {

pagerequest = new ActiveXObject("Msxml2.XMLHTTP")

}

catch (e){

try{

pagerequest = new ActiveXObject("Microsoft.XMLHTTP")

}

catch (e){}

}

}

else

return false

pagerequest.open('GET', url, false) //get page synchronously

pagerequest.send(null)

writecontent(pagerequest)

}

function writecontent(page_request){

if (window.location.href.indexOf("http")==-1 || pagerequest.status==200)

document.getElementById("page1").innerHTML = pagerequest.responseText;

}

It's throwing an error on line 7 -- var pagerequest = false;

If you comment it out, it just throws an error on the next line. Any ideas?

Thanks in advance for your help!!

网友答案:

Yopur writecontent is wronge (argument naming) try:

function writecontent(page_request){
    if (window.location.href.indexOf("http")==-1 || page_request.status==200)
    document.getElementById("page1").innerHTML = page_request.responseText;
}

Also, there's no real value to this:

var pagerequest = false;

Since you never return it without setting it somewhere else in your code might as well just be:

var pagerequest;
网友答案:

In your writecontent function, you call the argument page_request, but then refer to it in the function body as pagerequest (without the underscore).

Otherwise, your code should be working -- see http://jsfiddle.net/2eynH/ for an example.

网友答案:

First things first - utilize http://jslint.com/

It does not like your writecontent function.

And pagerequest = new XMLHttpRequest() is missing a semicolon.

Also, I also like to "rip" my javascript through YUI Compressor to help reveal syntax errors.

http://developer.yahoo.com/yui/compressor/

Some more missing semicolons:

pagerequest = new ActiveXObject("Msxml2.XMLHTTP")
pagerequest = new ActiveXObject("Microsoft.XMLHTTP")

One more thing. Even though javascript allows you to do something, does not mean that you should. Declaring pagerequest as a boolean, then setting it to an ActiveXObject is a little confusing. I would probably initialize it to null. Then "test" for null later on down in the code.

网友答案:

You are missing semicolons on almost all lines.

Cleaned up code:

function ajaxinclude(url) {
    var pagerequest;

    if (window.XMLHttpRequest) { // if Mozilla, Safari etc
        pagerequest = new XMLHttpRequest();
    } else if (window.ActiveXObject){ // if IE
        try {
            pagerequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                pagerequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (ec) {}
        }
    }
    else {
        return false;
    }

    pagerequest.open('GET', url, false); // get page synchronously 
    pagerequest.send();
    writecontent(pagerequest);
}

function writecontent (page_request) {
    if (window.location.href.indexOf("http") == -1 || page_request.status == 200) {
        document.getElementById("page1").innerHTML = page_request.responseText;
    }
}
网友答案:

Your code is not valid. Semi-colons are added to your code when doesnt have it or it thinks it should have it.

So in

function ajaxinclude(url) 
{
    var pagerequest = false;
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        pagerequest = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
        try {

the javascript compiler will do the following

function ajaxinclude(url); // note the semi-color meaning the { starts floating in the middle of nowhere
{
    var pagerequest = false;
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        pagerequest = new XMLHttpRequest();
    else if (window.ActiveXObject){ // if IE
        try { //and so on

As most people have suggested run JSLint over it to see the mistakes.

Edit from comment

You can see the Semi-colon insertion details in the blog

分享给朋友:
您可能感兴趣的文章:
随机阅读: