0

So basically I have a link as a div in page 1.

<a href="/computing/abc/page2.php" class="topic-list-item">

    <h3 class="topic-title">Internet Protocol Basics</h3>

    <div class="topic-description-text">
        Learn how Internet Protocol is used 
    </div>
</a>

When I click on the div it opens the page 2 by reloading the page.

I want the new content of page 2 to appear without any reloading while at the same time this changes my URL.

Is this possible?

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
Neha
  • 149
  • 2
  • 16

1 Answers1

0

HTML:

Disable your <a> tag because you will send an ajax request instead of loading a new page

<a href="#" data-url="/computing/abc/page2.php" class="topic-list-item">

    <h3 class="topic-title">Internet Protocol Basics</h3>

    <div class="topic-description-text">
        Learn how Internet Protocol is used 
    </div>
</a>

JS (jQuery):

Bind the click event to execute your ajax call, then set the HTML your server send back where you want it to be.

  $("a").click(function(){
    $.ajax({
      url: $(this).data("url"),
      method: 'get',
      success: function(data){
         $("#targetContainer").html(data);
      }
    })
  });

The success function's argument will be what your server send back, the /computing/abc/page2.php page. You can set it where you need it to be on your page (here inside $("#targetContainer") ).

See Modify the URL without reloading the page for the url modification without reload

Community
  • 1
  • 1
Yabada
  • 1,728
  • 1
  • 15
  • 36