0

i'm having a problem with a bit of jquery right now. I've got a navigation code (EG pressing the up arrow and down arrow alters a $_GET variable in the URL). It's working fine in the positive numbers but the moment I try to go past -1 it unsets the variable and causes the page to reset. Below is the Jquery Function in question.

function arrowbuttons()
{
  $(document).keydown(function(e){
    var full_url = document.URL; // Get current url

    var split = location.search.replace('?, &');
    var y = split[7];
    var x = split[3];
    var down = +split[7]-1;
    var up = +split[7]+1;
    var left = +split[3]+1;
    var right = +split[3]-1;
    if (e.keyCode == 37) { 
       window.location = 'index.php?X='+left+'&Y='+y+'&Z=0'; 
       return false;
    }
    if(e.keyCode == 38) {
        window.location = 'index.php?X='+x+'&Y='+up+'&Z=0'; 
    return false;
    }
    if(e.keyCode == 39) {
        window.location = 'index.php?X='+right+'&Y='+y+'&Z=0'; 
    return false;}
    if(e.keyCode == 40) { 
    window.location = 'index.php?X='+x+'&Y='+down+'&Z=0'; 
    return false;}
  });   
}

If either of the effected URL variables (X or Y) are attempting to go below -1, like i said before, everything is redirected back to the start (X=0&Y=0&Z=0) by my php redirect code. This is strictly a Jquery issue, as the links that i've hardcoded into my site i'm fully capable of going as low as -16. Furthermore, I've noticed that I cannot go above +10 on any of the directions; when i go above +10 i resets me to (X=2 or Y=2 depending on which variable has been affected)

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • Sounds like you have an issue when the number is 2 or more characters, that could have something to do with the split code at the beginning? – Matt Dodge Oct 16 '13 at 23:13
  • Ah... I didn't think about that... Though, why would it wait until the number was 11 or -2?... I guess it might have something to do with the php that's handeling the code.... Is there any other option besides the split? – Patrick Pierson Oct 16 '13 at 23:17
  • hmm that is weird, sounds off-by-one-ish. The likely culprit is your string offsets after doing the replace (which looks wrong too?). I would `console.log` `x` and `y` after computing them each time, I bet you find out what's wrong then – Matt Dodge Oct 16 '13 at 23:53
  • @mattedgod Probably a good idea. I'm about to take an hour break from coding but when i get back i'll do that or what Barbara Laird said below... though having messed around with what she suggested, i'm clearly not quite at that level of Java/Jquery experience yet. – Patrick Pierson Oct 16 '13 at 23:57

1 Answers1

0

You've got a couple of issues happening here:

1st: replace takes 2 parameters. It looks for the first string and replaces it with the 2nd. You're call won't replace anything because it doesn't find the string "?,&" in your location.search string. So split will be X=1&Y=0&Z=0 (for example) after your replace. 2nd: you are hard coding the exact locations in the string that you are expecting the values. If any of the values are 2 digits or a negative number, it doesn't work because the values are no longer at split[7] and split[3]. Since the string is now X=-1&Y=10&Z=0 (for example)

This is a good discussion on getting query params in javascript How can I get query string values in JavaScript?, But I generally just have php set the variables in my javascript for me.

Adding a fiddle, for you to play with: http://jsfiddle.net/bhlaird/M3Ub7/

function getParameterByName(name,location_search) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location_search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var location_search = '?X=-1&Y=10&Z=0'  // replace this string with location.search
var y = getParameterByName('X',location_search);
var x = getParameterByName('Y',location_search);
var down = +y - 1;
var up = +y + 1;
var left = +x + 1;
var right = +x - 1;
Community
  • 1
  • 1
Barbara Laird
  • 12,599
  • 2
  • 44
  • 57