 |
 |
JavaScript problems
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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...
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status:
Offline
|
|
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
|
|
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...
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status:
Offline
|
|
Thank you very much for everyone's help. I learned a lot from you.
t
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|