duanheye7909 2015-02-25 16:42
浏览 287
已采纳

history.pushState删除部分网址

I'm trying to implement history.pushState into our website to replace hash bangs but I'm puzzled as to why every time I click on a link that instead of appending the href value onto the current url e.g. www.abc.com/123.php/20 it instead makes it www.abc.com/20 even though I am still on the 123.php page (hopefully that makes sense).

I should also point out the the html below is created via php.

I have read quite a few tutorials explaining how to implement this and read a lot of articles, but still no luck. Also all the tutorials I have found show this working with the main menu but this is working with a grid of projects that load in data with Ajax.

Any help would be hugely appreciated!

<a id="projectAnchor" class="projectItem powerline" href="30" style="position: absolute; left: 5px; top: 5px;">
    <div id="imgContainer" name="Western Power Distribution - Brechfa Connection" href="#!30">
        <img id="projectImg" src="projectContent/30/198-th.jpeg" alt="Western Power Distribution - Brechfa Connection">
    </div>
    <img id="projectIcon" src="images/solution/powerlineIconB.png" alt="Powerline">               
        <p id="projectText">Western Power Distribution - Brechfa Connection</p>
        <div class="blueBar" id="colourBar"> </div>
</a>

var e, p;
$(function(){
    p = $(".projectItem").click(function() {
        e = $(this);
        if(!e.hasClass('active')){
            getResults();
            history.pushState(null, null, e.attr('href'));
        }
        return false;
    });
});

window.addEventListener("popstate", function(){
    e = $('a[href="'+location.pathname.split("/").pop()+'"]');
    getResults();
});

function getResults(){
    p.removeClass('active');
    e.addClass('active');
    $.get('projectT.php', {id: e.attr('href')}, function(data){
        $('#projectContent').html(data);
    });
}

</div>
  • 写回答

2条回答 默认 最新

  • douminfu8033 2015-02-25 17:00
    关注

    pushState allows you to change the URL to any valid URL on the same origin.

    Your link (which you use as the basis to calculate what goes into pushState) says to go to 30. Your current URL is http://www.example.com/123.php. Standard resolution of relative URLs which don't begin with a / character is to discard the last path part and everything after it (/123.php) and then put the relative URL in its place.

    You should write your JavaScript so that as well as changing the URI you manipulate the DOM so the resulting page is the same as you would get if you had just visited http://www.example.com/20 in the first place.

    If you actually want to go to http://www.example.com/123.php/20 then you have to link to that: href="123.php/20".

    Of course, that could lead to you ending on up http://www.example.com/123.php/123.php/30 if you follow a link to 20 and then another to 30 so you probably want to use a URL relative to the root: href="/123.php/20".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵