PDA

View Full Version : Help me please with this code



RAN55
05-01-2007, 10:04 PM
Hi,

I have this code and I need to change the drop-down box to checkboxes.
Can you help me please?

Thank you in advace


<script type="text/javascript">

function Gsitesearch(curobj){
var domainroot=curobj.domainroot[curobj.domainroot.selectedIndex].value
curobj.q.value="site:"+domainroot+" "+curobj.qfront.value
}
</script>


<form action="http://www.google.com/search" method="get" onSubmit="Gsitesearch(this)">

<p>
<input name="q" type="hidden" />
<input name="qfront" type="text" style="width: 180px" /> <input type="submit" value="Search" /><br />
Search:
<select name="domainroot">
<option value="www.javascriptkit.com" selected="1">JavaScript Kit</option>
<option value="www.dynamicdrive.com">Dynamic Drive</option>
<option value="www.freewarejava.com">FreewareJava.com</option>
</select>
</p>

</form>

jscheuer1
05-02-2007, 05:21 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function Gsitesearch(curobj){
for (var i_tem = 0, dms=curobj.elements; i_tem < dms.length; i_tem++)
if(dms[i_tem].name=='domainroot'&&dms[i_tem].checked)
curobj.q.value="site:"+dms[i_tem].value+" "+curobj.qfront.value;
}
</script>
</head>
<body>
<div>
<form action="http://www.google.com/search" method="get" onsubmit="Gsitesearch(this)">
<p>
<input name="q" type="hidden" />
<input name="qfront" type="text" style="width: 180px" /> <input type="submit" value="Search" /><br />
Search: <br>
<input type="radio" name="domainroot" checked value="www.javascriptkit.com">JavaScript Kit<br>
<input type="radio" name="domainroot" value="www.dynamicdrive.com">Dynamic Drive<br>
<input type="radio" name="domainroot" value="www.freewarejava.com">FreewareJava.com<br>
</p>

</form>
</div>
</body>
</html>

Twey
05-02-2007, 08:00 PM
for (var i_tem = 0, dms=curobj.elements; i_tem < dms.length; i_tem++)
if(dms[i_tem].name=='domainroot'&&dms[i_tem].checked)Wouldn't it be easier and more efficient just to loop through curobj.elements.domainroot?
curobj.q.value="site:"+dms[i_tem].value+" "+curobj.qfront.value;Be careful of accessing elements as properties of the form. Problems can occur, and it's generally ugly. Use the elements collection instead.
<input name="q" type="hidden" />That's not valid HTML Strict... well, maybe with NET, but I don't think that's what you meant.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>A</title>
<script type="text/javascript" src="http://www.twey.co.uk/pythonic.js"></script>
<script type="text/javascript">
Twey.Pythonic.LOAD("Array.filter");
</script>
<style type="text/css">
form.searchform label {
display: block;
}
</style>
</head>
<body>
<form class="searchform" action="http://www.google.com/search" method="get" onsubmit="this.elements['q'].value = 'site:' + Array.prototype.slice.call(this.elements['domainroot']).filter('checked')[0].value + ' ' + this.elements['qfront'].value;">
<div>
<input name="q" type="hidden">
<input name="qfront" type="text" style="width: 180px">
<input type="submit" value="Search">
<fieldset>
<legend>Search...</legend>
<label>
<input type="radio" name="domainroot" checked value="www.javascriptkit.com">
JavaScript Kit
</label>
<label>
<input type="radio" name="domainroot" value="www.dynamicdrive.com">
Dynamic Drive
</label>
<label>
<input type="radio" name="domainroot" value="www.freewarejava.com">
FreewareJava.com
</label>
</fieldset>
</div>
</form>
</body>
</html>

jscheuer1
05-03-2007, 04:24 AM
Wouldn't it be easier and more efficient just to loop through curobj.elements.domainroot?

If your code is an example of that, no. At least not easier looking.


That's not valid HTML Strict... well, maybe with NET

The page validates as strict.

Twey
05-03-2007, 10:46 AM
The page validates as strict.Aye, but only with NET. Most browsers don't implement NET any more, so it'll be error-corrected. If they did, you'd find > characters scattered about the page:
<br />in SGML with NET is shorthand for
<br>&lt;Have a read of Sending XHTML as text/html Considered Harmful (http://www.hixie.ch/advocacy/xhtml).
If your code is an example of that, no. At least not easier looking.Looks fairly simple to me. Array.prototype.slice.call(this.elements['domainroot']) could be a bit intimidating, I suppose, but it's really only a way of saying "convert this.elements['domainroot'] to an array." Array.prototype.slice builds a new array to return, and it will work on anything that supports array-like access syntax: [0], [1], &c. Thus, it's the most efficient way to convert a collection to an array.

jscheuer1
05-03-2007, 03:30 PM
Validation is overrated. If it doesn't significantly add to the code's usability, it is nit picking. However, I hadn't intended to copy what even I consider the OP's errors there. That was code lifted directly from the OP's example. And, as I said, it validated. It should be noted as well that I was not using an XHTML DOCTYPE nor xml of any sort. If it truly is valid only in XHTML, it points up another mistake by the validator.

What looks simple to you doesn't necessarily look that way to others. I think this looks pretty simple:


function Gsitesearch(curobj){
for (var i_tem in dms=curobj.elements['domainroot'])
if(dms[i_tem].checked)
curobj.q.value="site:"+dms[i_tem].value+" "+curobj.qfront.value;
}

But, IE barfs on it and it really isn't all that simple of a concept to beginners or even to intermediate coders anyway.

How simple (or reliable for that matter) can it be if it relies upon code from your server in addition to what looks a bit bloated as an onsubmit event?

Twey
05-03-2007, 07:53 PM
How simple (or reliable for that matter) can it be if it relies upon code from your serverIt just simplifies using some fairly basic functions. The poster can download the file or even copy and paste the function in question into the script directly if s/he wants.
in addition to what looks a bit bloated as an onsubmit event?It's a single statement, about a fifth of the code yours uses.
And, as I said, it validated. It should be noted as well that I was not using an XHTML DOCTYPE nor xml of any sort. If it truly is valid only in XHTML, it points up another mistake by the validator.Like I said above, it's valid, but doesn't mean what you think it does.
Validation is overrated. If it doesn't significantly add to the code's usability, it is nit picking.Validation always improves usability. It future-proofs the code, at least in theory. In practice that might not always be the case, but it's always better than nothing.
IE barfs on itHmm... I'm fairly sure it shouldn't. Have you tried:
for (var i_tem in dms=(curobj.elements['domainroot']))? Either way, using for...in to walk arrays is usually a bad idea.

jscheuer1
05-04-2007, 04:08 AM
Quote:IE barfs on it

Hmm... I'm fairly sure it shouldn't. Have you tried:
Code:
for (var i_tem in dms=(curobj.elements['domainroot']))
? Either way, using for...in to walk arrays is usually a bad idea.

I'll give that a shot but, what was happening - at least as far as I could tell - was that IE was looping through all the properties of curobj.elements['domainroot'] including its objects but, was seeing each individual instance of:

curobj.elements['domainroot'][i_tem][value]

as undefined for some reason, even when it was an object with that attribute or property (I think I tried it both ways).

Back to your code's reliance upon code from your domain for a moment - It really isn't so simple for folks. I could deal with it easily I suppose, but with another method in hand that works without the need to track down code essentially hidden on another domain, I didn't even bother.

The bit about long inline events is a simple matter of taste. For me there is some gray area. At some point though, I prefer a slightly longer defined function than a long bit of code inline with the element. I admit my function could be streamlined a bit too. I don't think the OP could go too far wrong following either of our individual efforts at solving this (assuming of course that yours works - I didn't test it).

Twey
05-04-2007, 08:49 AM
Er, I meant to say:
for (var i_tem in (dms=curobj.elements['domainroot']))
I'll give that a shot but, what was happening - at least as far as I could tell - was that IE was looping through all the properties of curobj.elements['domainroot'] including its objects but, was seeing each individual instance of:

curobj.elements['domainroot'][i_tem][value]

as undefined for some reason, even when it was an object with that attribute or property (I think I tried it both ways).That's very strange. I'd expect it to barf when it hit a property like length and tried to read its value (hence why using for...in to loop through arrays is a bad idea), but it should still be defined for the elements...
At some point though, I prefer a slightly longer defined function than a long bit of code inline with the element.Hm, I still don't see a single statement as being "long." I can see what you mean: it is, after all, rather a long statement, but I tend to base my judgement of the length of code on how many statements/flow control structures it requires rather than just how many characters are in it. Tastes differ, I suppose, but it would be wasteful of resources to create a whole new function just to avoid embedding something inside an inline handler. Perhaps if you were to overwrite the browser-supplied function, it would be OK, but not when calling your function inline.
Back to your code's reliance upon code from your domain for a moment - It really isn't so simple for folks. I could deal with it easily I suppose, but with another method in hand that works without the need to track down code essentially hidden on another domain, I didn't even bother.Hmm... it's very difficult to balance convenience and usability. I quite like this solution: so long as I maintain backwards compatibility, I can simply link to that script and load a function rather than copying and pasting a large chunk of code into the script. It also allows me to rewrite one of my functions if I think of a more efficient way of doing it without editing every one of my posts using that function. The "loading" mechanism allows me to avoid cluttering up native prototypes with functions I don't intend to use, like some libraries (cough Prototype cough) do. I do see your point, though: the approach has its downsides. I just think that the benefits outweigh them in this case.