change function the loading of jquery pages for onclick

Asked

Viewed 347 times

1

I’m looking to improve the way pages load from my project.

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

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

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

The script that jquery what use for loading pages inside the box is:

$(document).ready(function(){ 
     $('.postss a').live('click',function(){
           $('#conteudo').load($(this).attr('href')); 
           return false; 
     });
});

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; 
     });
});
  • I understood the line of reasoning. I just did not use out-href, I used an id, so replace attr('href') by attr('id'). And in the link I added the id, ai was <a href="a. php" id="b. php">Link</a>

  • This out-href attribute is not standard HTML, so it can be "created" for your needs, I think it’s risky to use an existing attribute for a different context, mainly id, but if you’ve got the right reasoning

  • As @Jader commented in his reply, change the line where you take the element, instead of using . live use . on: $('.postss a').on('click', null, function(){

0

Have a way of doing without having to modify the links on 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 function jaw_href to add the parameter at the end of the URI, if you already have parameters concatenate with & contrary to ?.

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

When the user tells you to open in a new tab, or hold Ctrl and click on the link, it will open the original link without ajax=true.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.