0

It works for me but I need it to open in the same window and not in a new window.


This is the code:

    <style>
        #counter {
  padding: 20px;
  background: red;
  text-shadow: 1px 1px 1px #202020;
  font-family: "Lato", sans-serif;
  font-size: 18pt;
  border: 1px solid lightblue;
  color: lightblue;
}
    </style>
<?php

    $counterFile = 'counter.txt' ;

    // jQuery ajax request is sent here
    if ( isset($_GET['increase']) )
    {
        if ( ( $counter = @file_get_contents($counterFile) ) === false ) die('Error : file counter does not exist') ;
        file_put_contents($counterFile,++$counter) ;
        echo $counter ;
        return false ;
    }

    if ( ! $counter = @file_get_contents($counterFile) )
    {
        if ( ! $myfile = fopen($counterFile,'w') )
            die('Unable to create counter file !!') ;
        chmod($counterFile,0644);
        file_put_contents($counterFile,0) ;
    }

?>
<html>
    <head>
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script type="text/javascript">
            jQuery(document).on('click','a#download',function(){
                jQuery('div#counter').html('Loading...') ;
                var ajax = jQuery.ajax({
                    method : 'get',
                    url : '/data-itai.php', // Link to this page
                    data : { 'increase' : '1' }
                }) ;
                ajax.done(function(data){
                    jQuery('div#counter').html(data) ;
                }) ;
                ajax.fail(function(data){
                    alert('ajax fail : url of ajax request is not reachable') ;
                }) ;
            }) ;
        </script>
    </head>
    <div id="counter"><?php echo $counter ; ?></div>
    <a href="" id="download"  onclick="window.open(this.href);return false;">Download btn</a>
    <style>
        #counter {
  padding: 20px;
  background: red;
  text-shadow: 1px 1px 1px #202020;
  font-family: "Lato", sans-serif;
  font-size: 18pt;
  border: 1px solid lightblue;
  color: lightblue;
}
    </style>

Maybe it's something to do with it:

onclick="window.open(this.href);return false;"

THANKS FOR HELPERS!!!

Itai Itai
  • 27
  • 4

2 Answers2

0

Assign the URL to window.location to reload the current window instead of opening a new window.

onclick="window.location = this.href;return false;"

But this is the default action of clicking on a link, you don't need to use Javascript for it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Use like this to open in a new tab:

window.open("www.youraddress.com","_self")

Or

Replace the current URL:

location.replace("www.youraddress.com");
Zoe
  • 27,060
  • 21
  • 118
  • 148