【问题标题】:load content via hashchange + jquery based on file name not in hash根据不在哈希中的文件名通过 hashchange + jquery 加载内容
【发布时间】:2012-01-04 12:24:15
【问题描述】:

我正在使用 jquery + ben alman 的 hashchange 插件。以下是获取哈希名称并加载内容的标准方法

$(window).hashchange(function() {
var hash = location.hash;
var array_url = hash.split('#');
var page = $(array_url).last()[0];
$('#content').load( page + '.php', function(){
});
});

但是有没有办法通过抓取分配给点击函数或通过 php 排序的其他变量来做到这一点?

我正在与一个多艺术家作品集网站合作,该网站向图像分发唯一的三四字母代码

我想通过独特的网址提供这些图片。出于多种原因,这必须通过 ajax。

我在添加其他 ajax 历史记录选项时遇到了困难,因为此页面已经使用 ajax 分页(加载此内容)和许多 htaccess url modrewrites。

我认为这可能是不可能的。

这是我当前的代码

$('a.photo').click(function () {
    var url = $(this).attr('href'),
    image = new Image();
    image.src = url;
    var clickedLink = $(this).attr('id');
    location.hash = clickedLink;
    image.onload = function () {
         $('#content').empty().append(image);
    };
    image.onerror = function () {
       $('#content').empty().html('That image is not available.');
    }
    $('#content').empty().html('Loading...');
    return false;
});

$(window).hashchange( function(){
    var hash = location.hash;
    var url = ( hash.replace( /^#/, '' ) || 'blank' );
    document.title = url;
})

$(window).hashchange();

还有我的html/php:

$thethumb = customuniqueidfunc();

<a href="[IMG URL]" 
class="photo gotdesc nohover" rel="<?php echo $row['description'] ?>" 
id="<?php echo $thethumb; ?>">

只要来自href attr 的图像加载到#content div 并且来自id attr 的散列作为散列添加到 url 和页面标题,这就是有效的,但我是缺少任何机制来组合 click 和 hashchange 函数,以便每个哈希实际上对应于图像。

【问题讨论】:

  • 我对您具体要求或尝试做什么感到困惑。你能澄清一下你的目标吗?
  • 对不起-我正在尝试使用可收藏的 ajax 加载内容,带有后退按钮,使用 hashchange,但连接 hashchange 以获得另一个变量,一个不是哈希的变量......我希望有帮助,但恐怕没有
  • 那么您具体需要什么帮助呢?您需要帮助使其可收藏/历史友好吗?此外,您是否需要“挂钩 hashchange 以获取另一个变量”的帮助?如果是这样,什么变量,“get”是什么意思?我想帮助你我只需要澄清这些事情,谢谢你的解释。
  • @JonathonG 嘿,希望你仍然乐于助人 :) 我需要帮助的是触发 hashchange 以将内容加载到 div 中,而不是基于哈希,而是像你说的另一个变量- 在 php 中生成的唯一 id 存储在链接的 id attr 中。最终用途 - 用户点击图像(href attr = img 和 id attr = id),href img 加载到 div 中,id attr 加载到哈希中。您可以在whitecu.be/user/mountain 看到上面的代码示例,谢谢!
  • 是的,仍然有心情。我会尽快处理并尽快回复您。

标签: php jquery ajax hashchange


【解决方案1】:

我之前为此使用过的一种方法是运行哈希轮询函数。您可以在此页面上看到它的实际效果:

http://www.webskethio.com/#services

这是该页面的 javascript:

http://www.webskethio.com/ws.js

相关代码:

function pollHash() {

    //exit function if hash hasn't changed since last check
    if (window.location.hash==recentHash) {
        return; 
    }
    //hash has changed, update recentHash for future checks 
    recentHash = window.location.hash;

    //run AJAX to update page using page identfier from hash 
    initializeFromUrl(recentHash.substr(1));

}

$(document).ready(function(){

    /* code removed for readability */ 

    setInterval('pollHash()',100); // Important piece

    /* code removed for readability */


});

//AJAX function to update page if hash changes
function initializeFromUrl(fromLink) {

    /* code removed for readability */


    //take hash from function call or from the URL if not
    input = fromLink || window.location.hash ;

    //remove # from hash
    output = input.replace("#","");



    //get the URL of the AJAX content for new page
    pageId = output;






var url = $(this).attr('href'),
image = new Image();
image.src = url;
var clickedLink = $(this).attr('id');
location.hash = clickedLink;
image.onload = function () {
     $('#content').empty().append(image);
};
image.onerror = function () {
   $('#content').empty().html('That image is not available.');
}
$('#content').empty().html('Loading...');       




}

[编辑:] 如果您可以创建一个仅从数据库中输出图像位置的页面,那么这是您的页面的完整代码。

var recentHash = "";
var image_url ="";

$(document).ready(function() {

    $('a.photo').click(function (event) {
        var clickedLink = $(this).attr('id');
        location.hash = clickedLink;

        event.preventDefault();
    });


    setInterval('pollHash()',100);

});

function pollHash() {

    //exit function if hash hasn't changed since last check
    if (window.location.hash==recentHash) {
        return; 
    }
    //hash has changed, update recentHash for future checks 
    recentHash = window.location.hash;

    //run AJAX to update page using page identfier from hash 
    initializeFromUrl(recentHash.substr(1));

}


//AJAX function to update page if hash changes
function initializeFromUrl(fromLink) {

    /* code removed for readability */


    //take hash from function call or from the URL if not
    input = fromLink || window.location.hash ;

    //remove # from hash
    output = input.replace("#","");



    //get the URL of the AJAX content for new page
    pageId = output;
    if(pageId != ""){
        var temp_url = 'http://whitecu.be/user/mountain/'+pageId+'.html';
        $.get(temp_url, function(data) {

            image_url = data;
            image = new Image();
            image.src = image_url;

            image.onload = function () {
                $('#content').empty().append(image);
            };
            image.onerror = function () {
                $('#content').empty().html('That image is not available.');
            }
            $('#content').empty().html('Loading...');       

        });



    }else{

        window.location = "http://whitecu.be/user/mountain";

    }

}

【讨论】:

  • 非常感谢您的帮助!我还是有点困惑。我了解 pollhash 函数,并且它每 100 毫秒运行一次以寻找新的哈希值。但是这是什么pageId = output;,什么是output
  • 我只是直接从我的脚本中发布了代码,删除了一堆东西。您可以替换 $("hiddenContent).fadeout.... 并继续使用您当前的代码将图像加载到 div 中。我已经在我的回答中为您完成了这项工作,但它可能需要一些调整您的部分,我希望这可以帮助您了解如何使用它。
  • @user1061301 如果你这样做,你实际上可以从 .click() 中删除那些东西,然后改变哈希值,当轮询函数看到哈希改变了。 (这发生得太快了,用户没有注意到延迟)
  • 再次感谢您的帮助!不幸的是,我还没有完全弄清楚。我已经添加了这个点击功能,但是当我更改哈希甚至手动输入它时,什么也没有发生。我在 intializeFromUrl 函数中设置了一个警报,但它从未触发,这让我认为它没有被调用...$('a.photo').click(function (event) { var clickedLink = $(this).attr('id'); location.hash = clickedLink; event.preventDefault(); });
  • 你能在 stackoverflow 聊天室里和我聊天吗? chat.stackoverflow.com/rooms/4823/chit-chat
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2012-09-07
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多