0

I have some code I copied from an example and I am not certain I understand it, and it is giving me an error that didn't happen when I first used is.

On this page:

http://www.comehike.com/hikes/uncancel_hike.php?hike_id=30

I get the error: x is not defined on line 84

I am not too certain what x is supposed to be there. Any help appreciated, especially in helping me understand what is happening here :)

Here is the code:

function getCall(fn, param)
{
   return function(e)
   {
      e = e || window.event;
      e.preventDefault(); // this might let you use real URLs instead of void(0)
      fn(e, param);
   };
}

window.onload = function()
{
   var a = document.getElementById("mylink");
   a.onclick = getCall(deleteHike, x );
};
Genadinik
  • 18,153
  • 63
  • 185
  • 284

2 Answers2

2

In your code here x is a parameter that will be passed into the function deleteHike and should be defined, or left null if you do not want a parameter, not knowing what deleteHike does here.

Your code will wire up an event when the page is loaded to the element myLink. When that element is clicked even info from the browser along with an extra parameter (x) will be passed into the function named deleteHike. Does that explain it?

Jay
  • 6,224
  • 4
  • 20
  • 23
  • +1 I'd expect that a `hikeID` of some sort is the parameter. – Tomalak Jun 10 '11 at 16:01
  • yes that helps a lot. But I am not sure then why this error happens on that page: Uncaught ReferenceError: x is not defined - it seems to be what is breaking the script. – Genadinik Jun 10 '11 at 16:05
  • As others have commented, in your code `x` just comes out of now where. It is never defined, so it doesn't know what to do with it. Find out what `deleteHike` does and what it needs for a parameter, then replace `x` with that. – Jay Jun 10 '11 at 16:11
  • After looking at the site with firebug, I'm pretty sure he needs to pass the unique id for a hike, which can be found in the "hike_id" parameter attached to the url – Kevin Bowersox Jun 10 '11 at 16:18
0

Your calling the function in your onload method and it doesn't know what the value of x is because it is not specified in the Javascript. I did notice the hike id was contained as a parameter in your url. Try this:

window.onload = function()
{
   var a = document.getElementById("mylink");
   a.onclick = getCall(deleteHike, gup("hike_id")); //notice this changed
};

//Add this function in the js
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

Also I noticed the A tag was getting the proper binding on the dom I'm not sure if you hardcoded that or what but if your view is being dynamically generated on the server this is all irrelevent and you don't need to included the line giving you the problems in your js just delete it in that case

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 1
    Don't forget to link your sources. Your `gup` function is from [this question](http://stackoverflow.com/q/901115/94197). There are also more concise and efficient methods than the accepted solution, which really only has the most up votes because it has the best visibility (being accepted). – Andy E Jun 10 '11 at 16:27