-2

My site don't want to show this script. When I put it on the index page it shows but when I put it on other pages nothing happens

<div id="pages"></div>  
<script>  
    a = location.href;  
    b = a.split('-');  
    c = b.length;  
    d = eval(c - 1);  
    e = b[d];  
    f = a.split('-' + e)[0];  
  
    $('#pages').html('<a id="b" href="' + f + '-' + eval(e - 1) + '">Previous' + 
        '</a><span id="i"> | </span><a id="a" href="' + f + '-' + eval(e * 1 + 1) + '">Next</a>');  
 
    $.ajax({  
        type: 'GET',  
        url: f + '-' + eval(e * 1 + 1),  
        error: function(){
            $('#a, #i').hide();
        }  
    });  

    $.ajax({  
        type: 'GET',  
        url: f + '-' + eval(e - 1),  
        error: function(){
            $('#b, #i').hide();
        }  
    });  
</script>

Can someone help?

  • Have you tried using ` – Aiias Jun 28 '13 at 05:36
  • 3
    Can you be more specific when you say "don't work"? What is expected and what exactly is happening? – Frederik.L Jun 28 '13 at 05:36
  • what is the value of `location.href` – Arun P Johny Jun 28 '13 at 05:38
  • Because you need to put in documen.ready or pageshow or pagebeforeshow events. like – Subhash Sasidharakurup Jun 28 '13 at 05:39
  • when i say dont work i mean it whont show "Previou / Next" pages on the site – Domokos Levente Jun 28 '13 at 05:39
  • 1
    Why are you using eval? Why are you generating `onclick` attributes as strings? Why are you using the most uninformative variable names possible? Why are you using globals? Why are you using variables with the same names as ids? All of these things make your code very hard to understand. – Quentin Jun 28 '13 at 05:51
  • eval() isn't really needed or intended for simple math, and actually eval() is almost never used in well written code. You can replace eval(c - 1) by c-1 – Paul Jun 28 '13 at 06:06

2 Answers2

1

I think the most important mistake in your code is that it's a real mess to read, and so it is to write. Here is some advice to solve your issue:

  • Remove all the eval hacks;
  • Name your variables so they mean something;
  • More lines doesn't mean better, ensure that each instruction is necessary;
  • Put your script in the header so it's easier to read;
  • As Subhash pointed out, make sure you don't access elements before they can exist.

This should solve your issue by itself. If not, please update your question with a more human-friendly code and I will be happy to investigate into it :-)

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” –Brian Kernighan

Frederik.L
  • 5,522
  • 2
  • 29
  • 41
-1

Well, just addressing the issue of getting the links to show up in the first place, my first approach would be to verify that the pages where the script does not work contains the necessary elements (in this case, an element with the id tag 'pages').

Then I'd make the script run only after the rest of the document has finished loading. See this for more details.

If you still run into issues, the next thing I'd do is open it in Chrome or Firefox and start using the DOM inspector and Javascript debugger just to "see" what exactly is happening as the code runs. It's short enough it would not be too tedious, and this is in my opinion one of the best ways to learn.

Other than that, I can't really offer much. You don't give enough information for me at least to know what problem you might be having. If you can narrow down the question yourself, we might be able to help you better.

Community
  • 1
  • 1
galdre
  • 2,319
  • 17
  • 31