1

I'm using a script to use AJAX on my blogger (https://github.com/swook/jquery-ajaxify-plugin) so users can listen music via the music player without interruption while they're navigating through differents pages.

Everything works except the random button. I'd like to AJAX-ify the random button.

The problem is when I click on the link, the page reloads.

You can check the different problems here: http://www.julienlussiez.com/p/test_20.html (There're 3 random buttons with different script)

Test #1: I have this [I tried many things on this code but nothing seems to work]:

     <div id="myLuckyPost"><a href="#random" onclick="feelingLucky();return false;" title="Aléatoire">Aléatoire</a></div>
<div id='head'></div>

<script type='text/javascript'>
//<![CDATA[
function showLucky(root){
    var feed = root.feed;
    var entries = feed.entry || [];
    var entry = feed.entry[0];
      for (var j = 0; j < entry.link.length; ++j) {
       if (entry.link[j].rel == "alternate") {
       window.location = entry.link[j].href;
       }
      }
   }

function fetchLuck(luck){
    script = document.createElement('script');
    script.src = '/feeds/posts/summary?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
   }
function readLucky(root){
    var feed = root.feed;
    var total = parseInt(feed.openSearch$totalResults.$t,10);
    var luckyNumber = Math.floor(Math.random()*total);
    luckyNumber++;
    fetchLuck(luckyNumber);
    }

function feelingLucky(e){
    var script = document.createElement('script'); 
    script.type = 'text/javascript';
    script.src = '/feeds/posts/summary?max-results=0&alt=json-in-script&callback=readLucky';
    document.getElementsByTagName('head')[0].appendChild(script);
    if(e && e.stopPropagation) {
        e.stopPropagation();
    } else {
        e = window.event;
        e.cancelBubble = true;
    }

    return false;
}
//]]>
</script>

Test #2 : With an other script for randomizing articles. The problem is still the same.

<script>
<!--
/*
Random link button- By JavaScript Kit (http://javascriptkit.com)
Over 300+ free scripts!
This credit MUST stay intact for use
*/

//specify random links below. You can have as many as you want

var randomlinks=new Array()

randomlinks[0]="http://www.julienlussiez.com/2013/01/le-repos-du-fou_21.html"
randomlinks[1]="http://www.julienlussiez.com/2012/11/dissonance-3.html"
randomlinks[2]="http://www.julienlussiez.com/2012/10/renaitre.html"
randomlinks[3]="http://www.julienlussiez.com/2013/01/defaillance.html"
randomlinks[4]="http://www.julienlussiez.com/2012/08/fragile-2012.html"

function randomlink(){
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
}

//-->
</script>
<form>
<p><input type="button" name="B1" value="Aléatoire" onclick="randomlink(); return false;" /></p> </form>

Test #3 : With an other script for randomizing articles. The problem is still the same.

<script>
/* Simple Random Posts function for Blogger
 * Copyright 2012 Yu-Jie Lin
 * Licensed under the MIT License
 */
function blogger_random_post(){
  // You can replace the value with your blog ID
  var blogID = $('#blogID').val();
  // for example:
  //  blogID = "4586487713356156869";
  var BLOGGER_POSTS_API = "http://www.julienlussiez.com/feeds/posts/summary";
  // appending necessary parameters
  BLOGGER_POSTS_API += "?alt=json-in-script&max-results=1&callback=?";
  // replacing blogID with value
  BLOGGER_POSTS_API = BLOGGER_POSTS_API.replace('blogID', blogID.toString());
  $('#btn-blogger-random').text('Tossing dice...');
  // retrieving posts count
  $.getJSON(BLOGGER_POSTS_API, function(data){
    var posts_count = parseInt(data.feed.openSearch$totalResults.$t);
    // index is 1-based
    var index = Math.floor(Math.random() * posts_count) + 1;
    // retrieving post link
    $.getJSON(BLOGGER_POSTS_API + '&start-index=' + index, function(data){
      $.each(data.feed.entry[0].link, function(idx, link){
        if (link.rel == 'alternate')
          // redirecting
          document.location.href = link.href;
      });
    });
  });
}
$(function(){
  $('#btn-blogger-random').click(blogger_random_post);
});
</script>
<label>Blog ID <input id="blogID" value="4586487713356156869"/></label>
<button id="btn-blogger-random">I'm Feeling Lucky &raquo;</button>  

Thanks! It would be very nice if you'd have some clues!

Julien
  • 25
  • 1
  • 8
  • Now, you have two `feelingLucky` function! The second one would overwrite the first one! – JoDev Mar 19 '13 at 10:00
  • Ok, I've just edited the code. Now, I'm back to the beginning. I don't know what I have to change to stop refreshing. – Julien Mar 19 '13 at 12:12
  • If you comment the `document.location.href = link.href;` treatment, is it still refreshing? And in your new `blogger_random_post`, I can't see where do you add something into the DOM! Can you show me a Json example of what *post link* is? – JoDev Mar 20 '13 at 08:25
  • You can check it on the bottom of the article. The page is still refreshing. – Julien Mar 20 '13 at 11:08

2 Answers2

0

You have to stopPropagation.

For that, you can add return false; to the end of the function.

See How to stop propagation

And if it doesn't work, you can add

// event is the Event object
if (event.preventDefault) {
  event.preventDefault();
}
event.returnValue = false;

See Alsacreation (in french ^^)

The event is an object you must pass to the caller...

<div id="myLuckyPost"><a href="#random" onclick="feelingLucky(event)" title="">BD Aléatoire</a></div>

function feelingLucky(event){
  //now you can use event.stopPropagation...

}

You also can retrieve this object by using window.event, but it's not cross browser! See Other topic on Javascript Event object

Community
  • 1
  • 1
JoDev
  • 6,633
  • 1
  • 22
  • 37
  • It doesn't work. The random button keep refreshing the page. Thanks anyway! I'm going to try again...maybe I don't write the code correctly. – Julien Mar 19 '13 at 09:26
  • Maybe with `javascript:void(0)` or `javascript:void('something')` in the `
    – JoDev Mar 19 '13 at 09:49
  • No...it doesn't work. I've just edited my post to show the code now. Maybe there's a problem. Thanks again! – Julien Mar 19 '13 at 09:56
  • Your code works too on Blogger but I just have "Somethink will be added" when I click on the random button. What should I change in the code? – Julien Mar 19 '13 at 11:40
  • Just replace the Fiddle's feelingLucky function with your's. – JoDev Mar 19 '13 at 12:50
0

Just change your existing feelingLucky function to

function feelingLucky(e){
    var script = document.createElement('script'); 
    script.type = 'text/javascript';
    script.src = '/feeds/posts/summary?max-results=0&alt=json-in-script&callback=readLucky';
    document.getElementsByTagName('head')[0].appendChild(script);
    if(e && e.stopPropagation) {
        e.stopPropagation();
    } else {
        e = window.event;
        e.cancelBubble = true;
    }

    return false;
}

If it doesn't work, youre problem is in the '/feeds/posts/summary?max-results=0&alt=json-in-script&callback=readLucky' page or in the ajaxify.js

IDEA

You can try to change your <a> to another element like <span>.

JoDev
  • 6,633
  • 1
  • 22
  • 37
  • Ok! Thanks. I've tried but it still doesn't work. Yes, maybe the problem is in the '/feeds/posts/summary?max-results=0&alt=json-in-script&callback=readLucky' page or in the ajaxify.js. – Julien Mar 19 '13 at 17:55
  • I'm trying an other script (EDIT). I have the same problem but maybe there's a solution on this one. I don't know what to modify. – Julien Mar 19 '13 at 22:55