How to stop a anchor tag redirection ?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
mukunthan
Forum Newbie
Posts: 12
Joined: Sat Sep 13, 2008 12:52 am

How to stop a anchor tag redirection ?

Post by mukunthan »

Hi,

I have a toplink
<a href="sow_lock.php" onclick="return chkBeforeLocking('S1')">Lock</a> on my page.

I want to stop it from going to the page(sow_lock.php) if AJAX response returns an error message. But it always goes to the page after displaying the error alert message.

function chkBeforeLocking(val){

if(val != ""){

var vresponse ;

http.open("GET", "chk_before_locking.php?sow_id="+val,true);


http.onreadystatechange = function(){

if(http.readyState == 4 ) { //&& http.status == 200

vresponse = http.responseText; alert(vresponse)


if(vresponse != "noerror"){

alert(vresponse);

return false;

}

}

}
http.send(null);


}
}

Kindly tell me why it does not stop if there is an error?

P.S : http is DOM object and the result of chk_before_locking.php page is either one of the following message
1.Pricesheet and Employee Roster are blank. Edit them before locking
2.Pricesheet is blank. Edit it before locking
3.Empsheet is blank. Edit it before locking
4.noerror
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How to stop a anchor tag redirection ?

Post by jackpf »

Hmm...I haven't actually read your code (I would if it were posted between

Code: Select all

 tags ;)) but you need to return false...that will always stop the browser performing it's default action for the event.
Post Reply