change function loading jquery pages to onclick

1

I'm wanting to improve the way pages load my project.

In this project I'm using jquery to load the page inside a box when I click on any of the links that I make available. In this script it uses the link that is in href to load the page.

I would like (if possible) to 'make up' this link with onclick . I want to make that if the person click directly on the link load the page in the box that should be loaded, but I also want if the person wants to open that link in a new tab it can open, only a different page. >

That is, in href I want to use a link where if the person clicks to open the page in new tab will open for example a.php page, now if the person clicks directly on the link load the page b. php via jquery inside the box.

The script that jquery that I use to load the pages inside the box is:

$(document).ready(function(){ 
     $('.postss a').live('click',function(){
           $('#conteudo').load($(this).attr('href')); 
           return false; 
     });
});
    
asked by anonymous 08.08.2014 / 18:35

2 answers

2

If I understand what you want, you can create a new attribute for it

<a href="a.php" out-href="b.php">Link label</a>

Then change your code to:

$(document).ready(function(){ 
     $('.postss a').live('click',function(){
           $('#conteudo').load($(this).attr('href')); 
           window.open($(this).attr('out-href'), 'nova janela'); 
           return false; 
     });
});
    
08.08.2014 / 19:22
0

There is a way to do it without having to modify the links in the pages:

jquery:

function jaw_href(href, parametros) { return encodeURI(href.replace(/#*/g,'') + (href.indexOf('?') != -1 ? "&" : "?") + parametros); }

$(document).ready(function() {

$("body").on("click", ".postss a", function(event) {
    event.preventDefault();
    $('#conteudo').load( jaw_href($(this).attr("href"), "ajax=true") ); 
});

});

Note: I created the jaw_href function to add the parameter at the end of the URI, if it already has parameters concatenated with & of the opposite with ? .

Then just check the $_GET['ajax'] variable and if necessary apply changes to ajax requests.

When the user has to open in new tab, or hold the ctrl and click on the link, will open the original link without ajax=true .

Attention to the function .live() it has been deprecated and has been removed in jquery 1.9 +

    
08.08.2014 / 20:46