2013-06-20 70 views
0

我有一个while循环,它创建了多个带有内部信息的表。 每个表格内都有一个脸书和推特图标。 读者可以通过onclick事件共享信息或发送表格内的信息。PHP中的Window.open while循环无法正常工作

这是每个事件。

<?php 
echo'<script type="text/javascript"> 
function tweet(){ 

var sharer = "https://twitter.com/intent/tweet?source=webclient&text=',$title2,' - http://www.globatum.com/lite/post.php?id=',$row['id'],'"; 
window.open(sharer,\'sharer\' , \'width=600,height=500\');} 
</script>'; 
?> 
<?php 
echo'<script type="text/javascript"> 
function fbshare(){ 

var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.globatum.com/lite/post.php?id=',$row['id'],'"; 
window.open(share,\'share\' , \'width=600,height=500\');} 
</script>'; 
?> 

奇怪的是,它的工作原理。有些。当我点击fb图标时,会弹出一个带有对话框的小窗口。同样的Twitter。 问题是,当我查看弹出窗口的标题时...它使用所有表中的相同信息。

我想要什么......

表1 FB点击图标应该打开窗口 https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1

表2将 https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=2

表3 .. https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=3 等等等等

但是当我点击图标时...

表1:打开窗口 https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1(GOOD)

表2:打开窗口... https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1(BAD)

表3:打开窗口... https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1(BAD)

这听起来像一个简单的SQL问题,但当我拉起页面源时,我得到

表1

<script type="text/javascript"> 
function tweet(){ 

var sharer = "https://twitter.com/intent/tweet?source=webclient&text=random title - http://www.makeupsite.com/post.php?id=39"; 
window.open(sharer,'sharer' , 'width=600,height=500');} 
</script><script type="text/javascript"> 
function fbshare(){ 

var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=39"; 
window.open(share,'share' , 'width=600,height=500');} 
</script> 

表2

<script type="text/javascript"> 
function tweet(){ 

var sharer = "https://twitter.com/intent/tweet?source=webclient&text=random title - http://www.makeupsite.com/post.php?id=43"; 
window.open(sharer,'sharer' , 'width=600,height=500');} 
</script><script type="text/javascript"> 
function fbshare(){ 

var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=43"; 
window.open(share,'share' , 'width=600,height=500');} 
</script> 

基本上页面的源代码是说一件事......但window.open做完全不同的事情。

回答

0

你一遍又一遍地重新定义相同的功能。根据浏览器的不同,您将始终调用您定义的tweet()或fbshare()的第一个或最后一个实例。

在您的文档的页脚,建立一个单一的功能:

function shareLink(targetURL){ 
    window.open(targetURL, 'share', 'width=600,height=500'); 
} 

然后,领带,为你的社交按钮:

onclick=shareLink("[twitter URL here]"); 

onclick=shareLink("[facebook URL here]"); 

看什么我正在?

编辑哎呦,帮助,如果我得到的名称权

+0

我的理解,这让有很大的意义。我如何通过onclick事件传递变量?以下方法不会打开一个新窗口。我应该尝试像onClick =“socialWindow(shareLink(这里的东西))”吗? –

+0

['code'] ['code'] 尝试过,并没有得到任何东西 –

+0

看到这个小提琴的工作示例:http://jsfiddle.net/cwTaT/1/ – CodeMoose