2011-05-04 48 views
1

好吧,也許我應該把我的所有代碼:javascript for onClick不執行?

<?php 
include('header_application.php'); 
$obj_clean->check_user(); 
$limit = 10; 
if(!isset($_GET['page'])) 
    $page = 1; 
else 
$page = $_GET['page']; 

$from = (($page * $limit) - $limit); 
$msg = ""; 
if (isset($_GET['unblock'])) 
{ 
    $code = $obj_clean->unblockUser($_GET['unblock'],$_GET['code']); 
    if ($code == "error") 
    { 
    $msg = "Could not delete message!"; 
    } 
    else 
    { 
    $msg = "You have unblocked ".$code; 
    } 
} 

//Get dynamic data required for this page from the database 
$users = $obj_clean->getContacts($_SESSION['user_id'], $from, $limit); 
$rows = $obj_clean->getContactsCount($_SESSION['user_id']); 
include ("header.php"); 
?> 
<div class="innerContainer"> 
<head> 
<script type="text/JavaScript"> 
function yesnolist(val) 
{ 
var e = confirm('Do you want to send a free chat request?'); 
if (e == true) 
{ 
    window.location.href = "http://www-rainbowcode-mobi/confirmfreechat.php"; 
    //window.location('http://www-rainbowcode-mobi/confirmfreechat.php'); 
    return true; 
} 
    else 
    return false; 
}  
</script> 
</head> 
<span class="headings2">CONTACTS</span> 
<?php if (isset($msg) && !empty($msg)) echo "<br/><font color='red'>".$msg."</font>"; ?> 
<br/><br/> 
<?php 
if (count($users) > 0) 
{ 
    echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>"; 
    foreach ($users as $user) 
    { 
     //Breaks the unique code into 3 parts so that the numeric part can be a different colour 
     $codeLength = strlen($user['unique_code']); 
     $firstPartLength = $codeLength - 5; 
     $uniqueCode3 = substr($user['unique_code'], -2); 
     $uniqueCode2 = substr($user['unique_code'], -5, 3); 
     $uniqueCode1 = substr($user['unique_code'], 0, $firstPartLength); 
     echo '<tr>'; 
     echo '<td>'; 
     echo '<a class="charcoal_link" style="line-height: 20px;" href="'.ADDRESS.'view_profile.php?id='.$user['profile_id_contact'].'">'.$uniqueCode1.'<span class="pink_text">'.$uniqueCode2.'</span>'.$uniqueCode3.'</a>'; 
     $requestor_id = $_SESSION['user_id']; 
     $profile_id = $user['profile_id_contact']; 
     $rel1 = $obj_clean->hasRelation($requestor_id,$profile_id); 
     $rel2 = $obj_clean->hasRelation($profile_id,$requestor_id); 
     if($rel1 && $rel2) 
     { 
      echo "&nbsp;&nbsp;"; 
      //echo '<a href="confirmfreechat.php?id='.$profile_id.'" onClick="yesnolist()">Free Chat</a>'; 
      echo '<a href="#" onClick="yesnolist(); return false;">Free Chat</a>'; 
     } 
     echo "</td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
} 
else 
{ 
    echo "You have no contacts yet"; 
} 
?> 
</div> 
<?php include("footer.php"); ?> 

希望這將有助於更好地

+1

你在你的錯誤控制檯得到什麼錯誤?爲什麼'head'之前有'div'元素?這是無效的。你的'body'標籤在哪裏開始? – 2011-05-04 09:40:35

+0

不知道這是否只是上述示例的問題,而您的實際代碼是正確的,但最後一行應該在塊內。此外,我引用Pekka:告訴我們在錯誤控制檯中會發生什麼,並寫出一個更乾淨的測試用例。 – gd1 2011-05-04 09:41:35

+1

@Pekka:'body'元素,先生,'body'元素。 [標籤不是一個元素。或者是嗎?](http://perfectionkills.com/tag-is-not-an-element-or-is-it/) – 2011-05-04 09:44:31

回答

0

試試這個:

function yesnolist() 
{ 
    if (confirm('Do you want to send a free chat request?')) 
     window.location = "http://www-rainbowcode-mobi/confirmfreechat.php"; 
} 

。 。 。

<a href="#" onClick="yesnolist()"> 
3
<a href="" onClick="yesnolist()">Free Chat</a> 

should be: 

<a href="#" onClick="yesnolist(); return false;">Free Chat</a> 
+0

(無論函數調用的結果如何,您都希望返回false,以便單擊事件被取消,因此在調用js函數之前頁面不會被留下) – Nathan 2011-05-04 09:44:45

0

我認爲改變URL以有效域可能有幫助的東西,所以嘗試:

window.location.href = "http://www.rainbowcode.mobi/confirmfreechat.php"; 
window.location('http://www.rainbowcode.mobi/confirmfreechat.php'); 
+0

我正在開發環境中從「哈希」 「:) – 2011-05-04 10:07:24

0
function yesnolist() 
{ 
    var e = confirm('Do you want to send a free chat request?'); 

    if (e == true) 
    { 
    window.location = "http://www-rainbowcode-mobi/confirmfreechat.php"; 
    return true; 
    } 
    else 
    return false; 
}  

<label onClick="return yesnolist();">Free Chat</a> 

你不可錯過的onclick事件(一個href =「」)標記,因爲href部分重定向頁面,然後調用javascript重定向部分。

或者

(a href="#" onclick="return yesnolist();")Free Chat(/a)