How to prevent reloading of a page after clicking on a hyperlink but redirect to that page and work out the necessary code written over there?

三世轮回 提交于 2019-12-06 07:16:10

So you need to load the page at the url but not navigate to it.

You can use .load() .

So in your case, lets say that your pop container div class is .popup, and the div where you want to load the urls content has an id say #container.

$(".popup a").on('click', function(e){
e.preventDefault();
var url = $(this).attr('delhref');

$("#container").load(url);

});

Few things to keep in mind,

  1. This will mostly not work for urls pointing to any other domain. (Same Origin Policy)
  2. Any content loaded with this function (.load()) shall be inserted within the container and all the incomming scripts if any shall be executed.

You can do that with history.js. Here is an example;

HTML:

<a href="/delete/1">Delete</a>
<a href="/update/2">Update</a>


<div id="content"></div>

JS:

var History = window.History;
if (!History.enabled) {
    return false;
}

History.Adapter.bind(window, 'statechange', function() {
    var State = History.getState();

    $('#content').load(State.url);
});

$('body').on('click', 'a', function(e) {

    var urlPath = $(this).attr('href');
    var Title = $(this).text();
    History.pushState(null, Title, urlPath);
    return false;
});

You can see code here: jsfiddle code

You can see demo here: jsfiddle demo

Note: Urls in example cannot be found. You need to update it according to your needs. This simply, loads content in href without refreshing your page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!