2013-06-20 65 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