它对我有用,但是我需要在同一窗口中打开它,而不是在新窗口中打开。
<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>
也许与它有关:
onclick="window.open(this.href);return false;"
感谢帮助者!
答案 0 :(得分:0)
将URL分配给window.location
以重新加载当前窗口,而不是打开新窗口。
onclick="window.location = this.href;return false;"
但这是单击链接的默认操作,您不需要使用Javascript。
答案 1 :(得分:0)
以这种方式在新标签页中打开:
window.open("www.youraddress.com","_self")
或
替换当前网址:
location.replace("www.youraddress.com");