JavaScript setFocus() - automatic 'focus on page load' - Smarty conflict?

robert070612

Joined: 2003-08-05
Posts: 565
Posted: Sat, 2005-10-15 23:17

Am trying to code automatic 'focus on load' for a text linker launch of the AdvancedSearch page. I understand this is normally done by JavaScript with which I'm not -that- familiar. My effort below does not do what is required but, at least, no longer causes an error.

*insert the onLoad clause

Quote:
[/templates/local/theme.tpl]
</head>
<body class="gallery" onLoad="setFocus()">
<div {g->mainDivAttributes}>

*insert the name clause as it seems to be missing, hopefully this is the correct name

Quote:
[modules/search/templates/block/local/SearchScan.tpl]
<form action="{g->url}" name="g2_form[useDefaultSettings]" method="post">

* insert the new JavaScript stuff

Quote:
[modules/search/templates/block/local/SearchScan.tpl]
{rdelim}
// ]]>
{literal}
function setFocus() {
document.g2_form[useDefaultSettings].g2_form[searchCriteria].focus();
}
{/literal}

</script>
<div class="gbBlock">

M$IE browser reports a JavaScript error in the status line.
FF browser's JavaScript console indicates 'not defined' errors.
Does this mean the above 'function' (defining?) stuff is not surviving Smarty?

Can anyone oblige me with a code fix or other helpful suggestion please?

----best wishes, Robert

[postedit: The drupal forum's search page code http://gallery.menalto.com/search isn't putting the cursor focus into its own new form box either. So even the forum isn't running the right code but that may be a different issue altogether I wouldn't know. Google's Advanced Search page does it every time http://www.google.co.uk/advanced_search and that's with the same kickstart of 'onLoad="setFocus()'. I've posted the code segments but there's something about G2 and/or Smarty that is stopping a standard JavaScript sequence running properly and the G2 advanced search form doesn't seem to have a 'name' either (bug report now filed). But what's the fix?]

[postedit: after some 5days thread continued into http://gallery.menalto.com/node/38712 where an alternative programming technique provided the required functionality]

 
test-engine

Joined: 2005-11-24
Posts: 1
Posted: Thu, 2005-11-24 14:39

Hi!
I hope the following instruction may help you
Change <body class="gallery" onLoad="setFocus()"> into <body class="gallery">
And the insert the following code at the end of your html page

Quote:
<script language="javascript">
document.g2_form[useDefaultSettings].g2_form[searchCriteria].focus();
</script>

I you still need my help, please email to

Regards
Dahn Terry

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sat, 2006-01-14 18:02

1. you should attach yourself to the body, don't add onLoad in the body tag, see:
http://gallery.menalto.com/node/19392

2. forms in g2 have an id, you can use the id instead of the name.