Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > JavaScript problems

JavaScript problems
Thread Tools
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 1, 2004, 05:56 PM
 
Hi I am having a problem with the following code:

Code:
var isFocused; function boardcode(code) { document.forms[0].body.value += code; document.forms[0].body.focus(); }
This is then called with a

onClick="boardcode('')

to add the color tag into a form field. For some reason Camino, Firefox and the new version of Safari all jump to the top of the page after inserting the code in the text field. So you have to scroll down manually to the text box once again.

Any ideas, why the page jumps after this?

t
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jul 1, 2004, 07:36 PM
 
Originally posted by t_hah:
Hi I am having a problem with the following code:

Code:
var isFocused; function boardcode(code) { document.forms[0].body.value += code; document.forms[0].body.focus(); }
This is then called with a

onClick="boardcode('')

to add the color tag into a form field. For some reason Camino, Firefox and the new version of Safari all jump to the top of the page after inserting the code in the text field. So you have to scroll down manually to the text box once again.

Any ideas, why the page jumps after this?

t
What is the onClick attached to? An <a href...> tag? If so, is the target an anchor, or what? Instead, make the target of the href like this:

[php]
<a href="javascript:boardcode('10')">
[/php]

Edit, for some reason the forum puts a space in "JavaScript", but you don't want it.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Jul 2, 2004, 02:47 AM
 
Try renaming the input to something other than 'body'. It might be that because it's a reserved word (the <body> tag) that it's causing problems...
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jul 2, 2004, 07:06 AM
 
Originally posted by Arkham_c:
What is the onClick attached to? An <a href...> tag? If so, is the target an anchor, or what? Instead, make the target of the href like this:

[php]
<a href="javascript:boardcode('10')">
[/php]

Edit, for some reason the forum puts a space in "JavaScript", but you don't want it.
AAARRGGHH!!!

Never put the 'java script' as the HREF. Very bad practice. Sorry Arkham, a pet peeve of mine (after countless websites where it interferes with real links). Avoid it like the plague.

Instead, try putting a button or an image, or some other 'clickable' element, and add the 'onclick' event to it:-

<button type="button" id="yourid" onclick="boardcode('10'); return false">Insert widget</button>

The 'return false' lets Javascript know to cancel the event after you've executed your code.

I suspect what was happening is that, as Black Books suggested, that the code was being executed then the A HREF was processed as usual - if it was the '#' then it simply returned you to the top of the page.
Computer thez nohhh...
     
t_hah  (op)
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 2, 2004, 08:52 AM
 
You guys are right.The problems was caused by the way href was used (with an achor).

We ended up placing an anchor right before the text box, and adding that anchor name in the href tag. Is this a good solution? So basically it looks somerthing like this:

Code:
<a href=#comment> <a href="#comment" onClick="boardcode('random text')">
Thanks,

t
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jul 2, 2004, 09:18 AM
 
Originally posted by t_hah:
You guys are right.The problems was caused by the way href was used (with an achor).

We ended up placing an anchor right before the text box, and adding that anchor name in the href tag. Is this a good solution? So basically it looks somerthing like this:

Code:
<a href=#comment> <a href="#comment" onClick="boardcode('random text')">
Thanks,

t
it should be <A name="comment" id="comment"></a> (or even delete the 'name' attribute if yer going XHTML strict)

But like I said, get rid of the <A> tag and just have the onclick event and you negate all this jiggery-pokery
Computer thez nohhh...
     
t_hah  (op)
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 2, 2004, 09:52 AM
 
Thank you very much for everyone's help. I learned a lot from you.

t
     
   
Thread Tools
Forum Links
Forum 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 On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:39 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2