如何AJAX-ify Blogger上的随机按钮?

时间:2013-03-18 14:08:34

标签: ajax button random blogger

我正在使用脚本在我的博客上使用AJAX(https://github.com/swook/jquery-ajaxify-plugin),这样用户可以在浏览不同的页面时通过音乐播放器不间断地收听音乐。

除随机按钮外,一切正常。我想AJAX-ify随机按钮。

问题是,当我点击链接时,页面会重新加载。

你可以在这里检查不同的问题:http://www.julienlussiez.com/p/test_20.html(有3个不同脚本的随机按钮)

测试#1:我有这个[我在这段代码上尝试了很多东西,但似乎没有任何工作]:

     <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>

测试#2:使用其他脚本随机化文章。问题仍然存在。

<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>

测试#3:使用其他脚本随机化文章。问题仍然存在。

<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>  

谢谢!如果你有一些线索,那将是非常好的!

2 个答案:

答案 0 :(得分:0)

您必须stopPropagation

为此,您可以将return false;添加到函数的末尾。

请参阅How to stop propagation

如果它不起作用,您可以添加

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

请参阅Alsacreation (in french ^^)

event是您必须传递给来电者的对象......

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

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

}

您也可以使用window.event检索此对象,但它不是跨浏览器!见Other topic on Javascript Event object

答案 1 :(得分:0)

只需将现有的feelingLucky功能更改为

即可
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;
}

如果它不起作用,则问题出在'/feeds/posts/summary?max-results=0&alt=json-in-script&callback=readLucky'页面或ajaxify.js

<强> IDEA

您可以尝试将<a>更改为<span>等其他元素。