tags:

views:

66

answers:

3
// javascript for ajax pagination//

document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {
    var img = new Image
    img.src = '/images/spinner.gif'

    function createSpinner() {
      return new Element('img', { src: img.src, 'class': 'spinner' })
    }

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
        el.up('.pagination').insert(createSpinner())
        new Ajax.Request(el.href, { method: 'get' })
        e.stop()
      }
    })
 }
})

Why is this script inserting two spinner images?

here is the html

 <div style="margin-top:25px;">
      <div class="pagination ajax">
           <span class="disabled prev_page">&laquo; Previous</span> 
           <span class="current">1</span> 
           <a href="/posts?page=2" rel="next">2</a> 
           <a href="/posts?page=3">3</a> 
           <a href="/posts?page=2" class="next_page" rel="next">Next &raquo;</a>
      </div>
      <br style="clear:both;"/>
 </div>
+1  A: 

Are there two elements with class=pagination?

morganpdx
no, you can check the html i posted on the question
Sam
@Sam: you did not include any markup in your question.
Matt Ball
This should be a comment. Not an answer.
patrick dw
@patrick, I agree, but I also see how it could be a simple gotcha answer
Sam
@patrick - thanks, point taken. But I was looking for the gotcha, considering there was no markup to look at.
morganpdx
+1  A: 

try to do this changes :

// javascript for ajax pagination//

document.observe("dom:loaded", function() { // the element in which we will observe all clicks and capture // ones originating from pagination links var container = $(document.body)

if (container) { var img = new Image img.src = '/images/spinner.gif'

var createSpinner() = function { 
  return new Element('img', { src: img.src, 'class': 'spinner' });
}

container.observe('click', function(e) {
  var el = e.element()
  if (el.match('.pagination a')) {
    el.up('.pagination').insert(createSpinner);
    new Ajax.Request(el.href, { 
      method: 'get',
      success: function(){ el.up('.pagination').find('img.spinner').remove(); }
    });
    e.stop()
  }
});

} });

Patrick Ferreira
please be more specific. Please take the original file and replace/change what you believe to be the problem.
Sam
no, thanks for the effort though
Sam
@Sam, Patrick Ferreira - The function creation would give a syntax error. If you were intending `var createSpinner = function()`, then it would be functionally equivalent to the question. And doing `el.up('.pagination').insert(createSpinner);` would be to attempt to insert the function itself, not the return value from calling the function.
patrick dw
oops! you write Sam!
Patrick Ferreira
+1  A: 

I ran the javascript through jslint and fixed all errors but new Ajax. I moved the createSpinner-function since it's not advisable to define it inside a block. Also the declaration of img was moved, semicolons added, and line-breaks to make the code more readable. (I have not tested the code, just "beautified" it)

document.observe(
  "dom:loaded",
  function() {
    var img;
    function createSpinner() {
      return new Element('img', { src: img.src, 'class': 'spinner' });
    }

    // the element in which we will observe all clicks and capture
    // ones originating from pagination links
    var container = $(document.body);

    if (container) {
      img = new Image( );
      img.src = '/images/spinner.gif';

      container.observe(
        'click',
        function(e) {
          var el = e.element();
          if (el.match('.pagination a')) {
            el.up('.pagination').insert(createSpinner());
            new Ajax.Request(el.href, { method: 'get' });
            e.stop();
          }
        }
      );
    }
  }
);
some
Just to be sure, its *perfectly* "legal" to declare a JS function within a block. However just like a var declaration, the function scope is the entire containing function, but unlike var, the function is defined regardless.
Angiosperm
@Angoiosperm: It executes, but it's confusing: `function load() { var container=1; if (container) { function createSpinner() { alert("me");} } createSpinner(); }` Can we agree that it's confusing and should be avoided?
some
No argument from me there.
Angiosperm
thanks, but it doesn't work. Look nicer though
Sam