making this js work in firefox?

  • Thread starter Sergei Shelukhin
  • Start date
S

Sergei Shelukhin

I have this IE compliant JS code, but it refuses to work in firefox. What's
up with it?
I am not any good in JS and I don't have a lcue on how to debug it apart
from alert-s, so I kinda don't even know hwere to start :(
I am using it for my browser custom start page, js is enabled...




function doSearch(url,qname)
{
url += ( url.indexOf("?") > 0 )?"&":"?";
url += qname + "=" + document.getElementById("q").value;
if ( document.getElementById("chkNewWindow").checked )
window.open(url,'_blank','');
else
location.href = url;
}

form is like this

<form id="searchform" action="http://www.google.com/search?hl=en"
method="get">
Search query: <input type="text" id="q" name="q" maxlength="1000"
size="40">
<br /><input type="checkbox" name="chkNewWindow"> Open new window
</form>
<ul style=" text-indent: 20px;">
<li><a
href="javascript:doSearch('http://www.google.com/search?hl=en','search');">S
earch Google</a></li>
<li><a
href="javascript:doSearch('http://www.merriam-webster.com/cgi-bin/dictionary
?book=Dictionary','va');">Search Merriam-Webster</a></li>
<li><a
href="javascript:doSearch('http://www.technorati.com/cosmos/search.html?rank
=','url');">Search Technocrati</a></li>

etc
 
M

Mick White

Sergei said:
I have this IE compliant JS code, but it refuses to work in firefox. What's
up with it?
I am not any good in JS and I don't have a lcue on how to debug it apart
from alert-s, so I kinda don't even know hwere to start :(
I am using it for my browser custom start page, js is enabled...




function doSearch(url,qname)
{
url += ( url.indexOf("?") > 0 )?"&":"?";
url += qname + "=" + document.getElementById("q").value;

if ( document.getElementById("chkNewWindow").checked )

if (document.forms["searchform"].chkNewWindow.checked) {

Your checkbox has no "id" attribute.
Mick
 
M

Michael Winter

On Wed, 10 Nov 2004 00:57:03 +0300, Sergei Shelukhin

[snip]
I am using it for my browser custom start page, js is enabled...

As in your own personal page?
function doSearch(url,qname)
{
url += ( url.indexOf("?") > 0 )?"&":"?";
url += qname + "=" + document.getElementById("q").value;
if ( document.getElementById("chkNewWindow").checked )

IE has a bug whereby document.getElementById will return a reference to an
element with a matching name attribute if no matching id can be found. It
should actually return null.

You don't need to use getElementById at all:

function doSearch(url, queryName) {
var elem = document.forms['searchform'].elements,
query = elem['q'].value,
newWin = elem['chkNewWindow'].checked;

url += ((-1 != url.indexOf('?')) ? '&' : '?')
+ queryName + '=' + query;

if(newWin) {window.open(url, '_blank');}
else {location.href = url;}
}

[snip]
<br /> [...]

Unless you're writing XHTML said:
<ul style=" text-indent: 20px;">

The left margin is probably what you want to adjust.

You shouldn't normally use the javascript: scheme, but as this is for your
own use, there isn't much harm.

[snip]

Hope that helps,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top