Reply
javascript newbie needs help!
Old 03-10-2005, 01:17 PM javascript newbie needs help!
Extreme Talker

Posts: 219
Location: UK, East Anglia
i know very little javascript so i might not make much sense
i have this which activates the javascript when someone clicks the link:

<a href="shop.php?id=1&item=5" onclick="buy()">item</a>

and this is the javascript thats in the head of my page,

<script type="text/javascript">
<!--
function buy() {
var answer = confirm("Are you sure you want to buy this item?")
if (answer){

} else{
window.location = "shop.php?id=1";
}
}
//-->
</script>

How do i make it so when the person clicks ok they will go to the page in the link(shop.php?id=1&item=5) and if they click cancel they will stay on the same page (shop.php?id=1)

Last edited by timsquash5 : 03-10-2005 at 01:22 PM.
timsquash5 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 03-10-2005, 02:35 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
HTML Code:
<a href="#" id="shop.php?id=1&item=5" onclick="if(confirm('U sure?')){window.location=(this.id);}">item</a>
__________________
Free Teacher Websites

Last edited by Phaedrus : 03-10-2005 at 02:49 PM.
Phaedrus is offline
Reply With Quote
View Public Profile
 
Old 03-10-2005, 03:45 PM
camperjohn's Avatar
Ultra Talker

Posts: 268
Location: San Diego
The better way to do it is to keep the link as your original, but return false if you don't want it to follow the link.

// Add return statement to onclick
<a href="shop.php?id=1&item=5" onclick="return buy();">item</a>

// Now make the buy function return the answer to the question
Code:
<script type="text/javascript">
<!--
function buy() 
{
  var answer = confirm("Are you sure you want to buy this item?");

  if (answer)
  {
    return true;
  }
  else
  {
    return false;
  }
}
//-->
</script>
It will do nothing if you return false; but will follow the link if you return true.

Always remember to put semicolons at the end of your script lines.

You can do this on one line if you want, the above is just for clairity.

<a href="shop.php?id=1&item=5" onclick="return confirm('Are you an idiot?');">item</a>
camperjohn is offline
Reply With Quote
View Public Profile Visit camperjohn's homepage!
 
Old 03-10-2005, 03:58 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
My example ensures the user has JS enabled, if that matters (which it probably doesn't)... otherwise camperjohn's method is simpler and better.
__________________
Free Teacher Websites
Phaedrus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to javascript newbie needs help!
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11764 seconds with 13 queries