Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2008, 02:19 AM   PM User | #1
pyramation
New to the CF scene

 
Join Date: Aug 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
pyramation is an unknown quantity at this point
How can I use AJAX without going back to the top of the page?

How can you use AJAX and an <A HREF tag and keep the page where it is, without going up to the top... and without using an <a name="somename">??

Code:
<a href="#" onclick="somefunction()"> Click Here </a>
pyramation is offline   Reply With Quote
Old 08-19-2008, 04:03 AM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Has nothing to do with Ajax. Just basic JavaScript canceling the default action of an element.

Code:
onclick="foo();return false;"
Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 08-19-2008, 07:06 AM   PM User | #3
pyramation
New to the CF scene

 
Join Date: Aug 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
pyramation is an unknown quantity at this point
Thanks for the reply! Much appreciated, but I still didn't get it to work quite yet... this is what I have:

Code:
<a href="#" onclick="somefunction();return false;"> Click Here </a>
The page still bounces back up to the top. If I remove the "#" from the href, then the function doesn't execute.

Any help is greatly appreciated.
pyramation is offline   Reply With Quote
Old 08-19-2008, 08:16 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
use

Code:
<input type="button" onclick="somefunction();" value="Click Here" />
to denote an action. links are for documents, buttons do stuff.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 08-19-2008, 12:35 PM   PM User | #5
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
If it is still going to the top, it sounds like you might have an error in your function.
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 08-19-2008, 04:09 PM   PM User | #6
pyramation
New to the CF scene

 
Join Date: Aug 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
pyramation is an unknown quantity at this point
The function could possibly have an error... its actually the function that was posted on "IE problems IE errors on page with AHAH", and I basically had separate issues with the same code.
The computer that produced that error is unavailable right now ( it was not mine ) ... so I should get the error message soon.

Thanks for all the replies!
pyramation is offline   Reply With Quote
Old 08-19-2008, 07:55 PM   PM User | #7
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
try onclick="this.href=location.href; somefunction();return false;"

you could also use onmousedown instead of onclick to cut the click off by about 100ms.

if you are worried about an error in the function, rethread the stack by using settimeout:


onclick="this.href=location.href; setTimeout(somefunction, 35); return false;"
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 08-19-2008 at 07:59 PM..
rnd me is offline   Reply With Quote
Old 08-19-2008, 10:19 PM   PM User | #8
pyramation
New to the CF scene

 
Join Date: Aug 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
pyramation is an unknown quantity at this point
Still didn't work, but the onmousedown is faster... thanks1
pyramation is offline   Reply With Quote
Old 08-20-2008, 07:22 AM   PM User | #9
SSJ
Regular Coder

 
Join Date: Mar 2007
Posts: 230
Thanks: 0
Thanked 4 Times in 4 Posts
SSJ is an unknown quantity at this point
You can use <label> instead of <a> to get it working
__________________
Best Joomla Design Agency India ||PSD to Joomla Template || Award Winning Web Design Company India
SSJ is offline   Reply With Quote
Old 08-20-2008, 01:32 PM   PM User | #10
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Quote:
Originally Posted by rnd me View Post
try onclick="this.href=location.href; somefunction();return false;"

you could also use onmousedown instead of onclick to cut the click off by about 100ms.

if you are worried about an error in the function, rethread the stack by using settimeout:


onclick="this.href=location.href; setTimeout(somefunction, 35); return false;"
Why in the world are you telling him to set the href of the link to the current page?

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 08-20-2008, 08:01 PM   PM User | #11
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
because he doen't want the link to do anything, and didn't care to follow it.
was thinking to use hash, but then what about the first visits....

if the link goes to where he already is, nothing will happen (was the theory).
i know it's unconventional, but my first two guesses didn't work.
rethinking about it, i bet some browsers will re-nav though if the link is an absolute url...

this is a prime example of why not use an 'a' tag for javascript actions, but hey, OP was determined.


its still not working, so if you have any suggestions, please feel free to share.


perhaps try:

Code:
onmouseover="this.href='javascript:void(0)' "
onclick=" somefunction(); return false;"
i still don't know why it's following the links in the first place, so that's just a stab.

you can also style a span to look like an a tag and use it instead. that would work for sure.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 08-20-2008 at 08:11 PM..
rnd me is offline   Reply With Quote
Old 08-21-2008, 12:06 AM   PM User | #12
pyramation
New to the CF scene

 
Join Date: Aug 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
pyramation is an unknown quantity at this point
I tried using span and label tags, no luck there, same behavior.
<br>
I tried:
Code:
onmouseover="this.href='javascript:void(0)' "
onclick=" somefunction(); return false;"
didn't work either...did you mean to put both of those in the same a tag? Thats what I did. Like this:
Code:
<a onmouseover="this.href='javascript:void(0)' " onclick=" somefunction(); return false;">
pyramation is offline   Reply With Quote
Old 08-21-2008, 09:40 AM   PM User | #13
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
post the code to somefunction().

it has to be something in that function.
there is nothing that a span tag would do to cause the behavior you describe.

i was wondering why the a tag wasn't working correctly...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 08-21-2008, 12:31 PM   PM User | #14
SSJ
Regular Coder

 
Join Date: Mar 2007
Posts: 230
Thanks: 0
Thanked 4 Times in 4 Posts
SSJ is an unknown quantity at this point
Use Following:

Quote:
<label onclick="somefunction()"> Click Here </label>
__________________
Best Joomla Design Agency India ||PSD to Joomla Template || Award Winning Web Design Company India
SSJ is offline   Reply With Quote
Old 08-21-2008, 10:31 PM   PM User | #15
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
please don't use the label tag for this. while it may work, as almost any other tag would, it is bad practice.

<label> tags are use to extend the focus grabbing of form elements.
that is all they are supposed to be used for.

why?

people with certain disabilities use software that interprets <label>s to mean that data goes to the input in the for attrib of the label.

you are possibly making the page confusing for these users by arbitrarily using it where a button, link, or input would make a lot more sense.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:24 AM.


Advertisement
Log in to turn off these ads.