0

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!!

2
  • the error is just a simple "syntax error." I have taken into account what the folks below have said and I am still getting errors. The problem wasn't with the writecontent() function, it is with the ajaxinclude() function. I also would like to mention that you do NOT need semicolons in JavaScript. Commented Sep 17, 2010 at 17:32
  • Usually these things happen when you copy&paste code from a website or a pdf document that uses fancy unicode characters to make the code look better. But I suppose the error message is wrong and the real problem is somewhere else. To check if it's a real syntax error, try putting some code outside of the function and see if it runs. If it runs then it's impossible for the function to contain a syntax error. In this case maybe there is a bug in the XMLHttpRequest implementation or a syntax error in the response body. Commented Jun 22, 2014 at 0:14

5 Answers 5

2

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

1 Comment

Semicolons are (generally) optional in Javascript, which does semicolon insertion automatically.
0

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;
    }
}

Comments

-1

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

3 Comments

Does it really do this? Are you saying that the opening brace must be on the same line as the function keyword? If so I've been doing it wrong forever.
@segfault Yea it does do that but if you minify your code normally you can get away with it. I have added a link to my answer showing it
No! A function statement always has a body. Inserting a semicolon before the body creates a syntax error. This is different to a return statement. The language dictates that you cannot put a line break between the return keyword and the return value expression. So inserting a semicolon before the line break eliminates a syntax error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.