views:

84

answers:

5

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

+1  A: 

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.

Kris Krause
Semicolons are (generally) optional in Javascript, which does [semicolon insertion](http://inimino.org/~inimino/blog/javascript_semicolons) automatically.
Daniel Vandersluis
+1  A: 

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;
Rudu
A: 

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;
    }
}
adamse
+1  A: 

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.

Daniel Vandersluis
A: 

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

AutomatedTester
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
@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
AutomatedTester