tags:

views:

34

answers:

2

i have a javascript file which has some code somewhat like this;

var Slide =     
{   

    init: function()
    {
     var thumbs = new Array();
     thumbs = Core.getElementsByClass("thumb-img");
     var i;
     for (i=0; i<thumbs.length; i++)
     {
      Core.addEventListener(thumbs[i], "click",  (function( j){return function(){Slide.showImage(j);/*alert(j);*/};})(i));

     }
    },

now i am trying to pass xmlhttp objects in this javascript file. i am adding the following just above the init: function()

var xmlhttp;

function getVote(int)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var value = int;
alert(value);
var url="user_submit.php";
url=url+"?vote="+int;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
  if (xmlhttp.readyState==4)
  {
  document.getElementById("captionbox").innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject()
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
return objXMLHttp;
}

but firebug gives me error: missing : after property id var xmlhttp;\n

what am i doing wrong?

+1  A: 

That's invalid syntax - you can't have code just floating around in an object. The easiest way to do that would be to just move that code outside the Object.

A better way would be to have a function initXhr(), which is called by init(). That would contain the same code as you tried to put before init. Then, add at the end:

this.xmlHttp = xmlHttp;

Then, in other functions, use this.xmlHttp.

Lucas Jones
even if i do that. still shows the same error.
amit
Could you paste your entire code to http://pastebin.com and post the link?
Lucas Jones
no wait, that works. sorry.
amit
No problem. Good luck! :)
Lucas Jones
hwo do i access the variables outside the Slide function, inside it?
amit
You should just be able to just use their name as usual, if you declared them with `var`.
Lucas Jones
but i am unable to. i want to use a var i made in getVote() in the Slide->init() function. how do i do that?
amit
Here, you must declare the var *outside* `getVote()`, then leave off the var when using it in getVote(). See http://pastebin.com/m87d7422
Lucas Jones
i dont follow. can you explain it a little.
amit
got the hang of it. thank you so much for your help. ;))
amit
A: 

It looks like you're putting normal Javascript inside an object block.

Move the XMLHTTP code block above the line var Slide = .

If this doesn't help, please show us the entire file.

SLaks