Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



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

Posts: 219
Location: UK, East Anglia
Trades: 0
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 02:22 PM..
timsquash5 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-10-2005, 03:35 PM
Phaedrus's Avatar
Ultra Talker

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

Please login or register to view this content. Registration is FREE

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

Posts: 268
Location: San Diego
Trades: 0
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>
__________________

Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
camperjohn is offline
Reply With Quote
View Public Profile Visit camperjohn's homepage!
 
Old 03-10-2005, 04:58 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
Trades: 0
My example ensures the user has JS enabled, if that matters (which it probably doesn't)... otherwise camperjohn's method is simpler and better.
__________________

Please login or register to view this content. Registration is FREE
Phaedrus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to javascript newbie needs help!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.13078 seconds with 12 queries