如何防止规范网址

How to prevent canonical URL's

本文关键字:范网址 何防止      更新时间:2024-02-10

我在home.php上有一个锚链接,它链接了这个:

<a href='#?id=$thought_id' class='toggle-comment' data-id='$thought_id' '> Comments ($num_of_comments) </a>

将鼠标悬停在此锚链接上时,我希望看到如下结果:

localhost/#?id=210

但我得到的是:

localhost/home.php#?id=211  

在这里看到了一个类似的问题:但是,在应用了最佳答案的建议后,我仍然得到相同的结果。我也有完全相同的锚链接存在于profile_page.php并且在那里完美运行。

锚链接并不意味着要去任何地方,单击时,它会动态放大其下方的div,显示评论。

编辑

锚链接的工作原理

  1. 单击时,锚链接会展开其下方的div。单击时,将显示此div:echo "<div id='toggleComment$thought_id' class='new_comment'>";

  2. 然后对于添加到此thought的每个新注释,另一个div被回显

    <div class='new_comm' id='thoughtId".$thought_id."-childId".$comment['id']."'>

JavaScript 来实现这一点

$(function() {
    $("a.toggle-comment").on("click", function(event) {
        // prevents browser to go to href's #
        event.preventDefault();
        // uses Jquery's data() function to get comment id from link's data-id attribute
        var id = $(this).data('id');       
        // get element by id and toggle display
        var ele = document.getElementById("toggleComment" + id);
        $(ele).toggle();
    });
});

编辑 2

和洛基聊了聊。问题是我定义了两次 JavaScript,一次在 home.php 的源代码中,一次在 functions.js 中,这也是在 home.phphead中。从源代码中删除脚本,代码开始运行。

#后面的所有内容都被解释为哈希片段,而不是发送到服务器。相反,您的浏览器会更改当前页面的哈希片段。

RFC 2396:统一资源标识符 (URI(:通用语法

字符"#"被排除在外,因为它用于分隔 URI来自 URI 引用中的片段标识符(第 4 节(。

https://www.rfc-editor.org/rfc/rfc2396

使用

<a href='/#?id=$thought_id' class='toggle-comment' data-id='$thought_id' '> Comments ($num_of_comments) </a>

相反(添加/在您的URL之前(