0

I have one web site with many random href <a> tags which used to redirect to another page. Is it possible to get which page the browser is trying to redirect when click the links with javascript and set return false and open that page with ajax and show it in a DIV? I am already trying with onclick but i need another easy way if possible.

Any answer appreciated.

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Shn
  • 368
  • 2
  • 10
  • 1
    possible duplicate of [With jQuery, How Do I Intercept Hyperlink Click Events Temporarily?](http://stackoverflow.com/questions/4384382/with-jquery-how-do-i-intercept-hyperlink-click-events-temporarily) – Pekka Jan 22 '14 at 14:33
  • 1
    This one has helpful code examples [jQuery: intercepting out going link and adding parameters](http://stackoverflow.com/q/2836380) – Pekka Jan 22 '14 at 14:34
  • 1
    See [this](http://css-tricks.com/snippets/jquery/target-only-external-links/). It'll help you isolate external links which you can bind to and do an event propagation prevention. – Prasanth Jan 22 '14 at 14:34
  • @Pekka웃 thanks for your quick response and answer it's much easier than i thought. – Shn Jan 22 '14 at 14:37

6 Answers6

2
<a href="link1" onclick="return false;" />Link A</a>
<a href="link2" onclick="return false;" />Link B</a>
<a href="link3" onclick="return false;" />Link C</a>
<a href="link4" onclick="return false;" />Link D</a>
1
    <div id="links">
        <a href="imalink-a" />Link A</a>
        <a href="imalink-b" />Link B</a>
    </div>

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

    $('#links').on('click','a',function(e){
        e.preventDefault();
        var link = $(this).prop('href');
        console.log(link);

        $( "#content" ).load( link );
    });
Simon Davies
  • 3,668
  • 9
  • 41
  • 69
1

yes it's possible.

$("a").on('click', function(e){
  // avoid the normal function of the <a>
  e.preventDefault();
  var href = $(this).attr("href");
  console.log(href);
  var current_location = $(location).attr('href');
  console.log(current_location);
  //make your ajax request here
  $("#google").load(href);
});
Ricbermo
  • 815
  • 1
  • 6
  • 28
0
 $("a").on('click',function(){
 event.preventDefault();
 url=$(this).attr('href');

 });

try this

Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30
0

http://jsfiddle.net/upsidown/J3Nvu/

$(".popup").click(function(e) {

    // Prevent browser from following the link
    e.preventDefault();

    // Get the href attribute value
    var url = $(this).attr("href");

    // Display it or do what you want here
    $("#result").html("Clicked " + url);
});
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
0

Link A Link B

 <div id="AjaxDiv"></div>

<script type="text/javascript">
 function AjaxLoad(URL)
  {
      $("#AjaxDiv").load(URL);
  }
</script>

                                **OR**


<div>
        <a href="javascript:void(0);" data-href="demo.aspx" class="ajaxload" />Link A</a>
        <a href="javascript:void(0);" data-href="2_demo.aspx" class="ajaxload" />Link B</a>
</div>
 <div id="AjaxDiv"></div>

<script type="text/javascript">
 $(document).ready(function() {
      $(".ajaxload").click(function() {
              $("#AjaxDiv").load($(this).attr("data-href"));
      });
 });
</script>
Prashant Kankhara
  • 1,498
  • 4
  • 15
  • 30