Freddy
Freddy

Reputation: 867

How to prevent canonical URL's

I have an anchor link on home.php which goes link this:

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

When hovering over this anchor link, I expect to see results like this:

localhost/#?id=210

But what I am getting it this:

localhost/home.php#?id=211  

I have seen a similar question here: But, having applied what the best answer suggests, I still get the same results. I also have the exact same anchor link present on profile_page.php and it works perfectly there.

The anchor link is not meant to go anywhere, on click, it dynamically enlarges the div below it, showing comments.

Edit:

How anchor link works:

  1. when clicked, the anchor link expands the div below it. When clicked, this div appears: echo "<div id='toggleComment$thought_id' class='new_comment'>";

  2. Then for each new comment added to this thought, another div is echo'd

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

JavaScript to achieve this:

$(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();
    });
});

Edit 2:

Came to a conclusion with Rocki in chat. The issue was that I have the JavaScript defined twice, once in the source code of home.php and once in functions.js which was also in the head of home.php. Removed the script from the source code, and code began to function.

Upvotes: 1

Views: 201

Answers (2)

Rocki
Rocki

Reputation: 363

Everything behind # is interpreted as the hash fragment and not send to the server. Instead your browser change the hash fragment for the current page.

RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax

The character "#" is excluded because it is used to delimit a URI from a fragment identifier in URI references (Section 4).

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

Upvotes: 2

crashxxl
crashxxl

Reputation: 679

use

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

instead (added / before your url)

Upvotes: 0

Related Questions