JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 8th, 2012, 10:29 PM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 53 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 3 m 27 sec
Reputation Power: 1
How would you guys avoid refreshing the page after JavaScript code is excuted.

Okay I have this code for a price calculator down below.

Every time I click each price, the prices add up to the total amount and it's okay.

However, when I have list of so many product that users would have to scroll down the page, they would be sent back to the top of the page after clicking "Add $xx" and I don't like this. Users may get tired of going back down and up while adding up products' prices. I want the user to stay at the same place after clicking "Add $xx" .

So does anyone know how to solve this problem?
Actually, why does this happen in the first place??





Code:

<script>
function total(amount)
{
var total = document.getElementById("total_storage").value;
total = parseInt(total) + parseInt(amount);
document.getElementById("total_storage").value="";
document.getElementById("total_storage").value = total;
document.getElementById("total_display").innerHTML = total;
}
function resetAll()
{
document.getElementById("total_storage").value = "0";
document.getElementById("total_display").innerHTML="0";
}
</script>

Total Price: $ <span id="total_display">0</span> || <a href="#" onclick="resetAll();return false">Clear Cart</a>

<br/>
<br/>

<input type="button" value="Add $10" onclick="total('10')"/>

<input type="button" value="Add $25" onclick="total('25')"/>

<input type="button" value="Add $100" onclick="total('100')"/>

<input type="button" value="Add $10" onclick="total('10')"/>

<input type="button" value="Add $25" onclick="total('25')"/>

<input type="button" value="Add $100" onclick="total('100')"/>

<input type="button" value="Add $10" onclick="total('10')"/>

<input type="button" value="Add $25" onclick="total('25')"/>

<input type="button" value="Add $100" onclick="total('100')"/>

<input type="button" value="Add $10" onclick="total('10')"/>

<input type="button" value="Add $25" onclick="total('25')"/>

<input type="button" value="Add $100" onclick="total('100')"/>

<input type="button" value="Add $10" onclick="total('10')"/>

<input type="button" value="Add $25" onclick="total('25')"/>

<input type="button" value="Add $100" onclick="total('100')"/>

<input id="total_storage" type="hidden" value="0"/>

Reply With Quote
  #2  
Old December 9th, 2012, 09:57 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 21 h 52 m 18 sec
Reputation Power: 1736
I tried your code with several lines, i.e. <br>, between each input and didn't got the browser to move the page back to the top.
I have tested in FF12 and IE9

Reply With Quote
  #3  
Old December 9th, 2012, 02:33 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
It is hard to tell without more of your code, but I guess clicking a button is causing a form submission. Try adding return false; just before the end of your total() function.

But you also create a variable called total - the same as your function name: don't do this!

Reply With Quote
  #4  
Old December 9th, 2012, 03:46 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,830 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 16 h 2 m 4 sec
Reputation Power: 4192
Quote:
Originally Posted by AndrewSW
But you also create a variable called total - the same as your function name: don't do this!

It could cause significant confusion, but they're in different scopes, so is it really that bad?
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #5  
Old December 9th, 2012, 04:39 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
Quote:
Originally Posted by Kravvitz
It could cause significant confusion, but they're in different scopes, so is it really that bad?


Revise that to "..I wouldn't do this"

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How would you guys avoid refreshing the page after JavaScript code is excuted.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap