jQuerybeast
jQuerybeast

Reputation: 14500

Search form, if no value go to site

I have a search form which searchers wikipedia.com. Nothing fancy, a form with an input.

If the input is empty, currently the user is redirected here: http://en.wikipedia.org/w/index.php?title=Special%3ASearch&redirs=1&search=&fulltext=Search&ns0=1 .

Id like to redirect the users here: www.wikipedia.com instead

How can I do this?

This is my search form:

<form method="get" action="http://en.wikipedia.org/w/index.php">
    <input type="hidden" value="Special:Search" name="title"/>        
    <input type="hidden" value="1" name="redirs"/>
    <input type="text" name="search">
    <input type="hidden" value="Search" name="fulltext"/>
    <input type="hidden" value="1" name="ns0"/>
</form>

Upvotes: 0

Views: 364

Answers (1)

David Thomas
David Thomas

Reputation: 253396

It's not possible, I don't think, without using JavaScript; if, as you've indicated, you're willing to use jQuery:

$('form').submit(
    function(){
        if ($(this).find('input:text').val() == ''){
            $(this).attr('action','http://www.wikipedia.com/');
        }
        else {
            $(this).submit();
        }
    });

JS Fiddle demo.

In plain JavaScript, the following seems to work (tested in Chromium 12 and Firefox 7 on Ubuntu 11.04):

var formEl = document.getElementsByTagName('form')[0];
var inputs = formEl.getElementsByTagName('input');
var textInputEl = [];
for(i in inputs){
    if (inputs[i].type == 'text'){
        textInputEl.push(inputs[i]);
    }
}

formEl.onsubmit = function(){
    if (textInputEl.value === '' || textInputEl.value === null){
        this.action = 'http://www.wikipedia.com/';
    }
    else {
        formEl.submit;
    }
};

JS Fiddle demo.


I'm trying to set this up so that, with an empty text-input, the form will remove any hidden elements and then set the action, or the document.location to be http://en.wikipedia.org/wiki/Special:Random. This does not, for some reason, seem to work. Almost as if Wikipedia's finding something wrong and then redirecting to a different page. Ungh. Anyways, this is what I tried:

 $('form').submit(
    function(){
        if ($(this).find('input:text').val() == ''){
            $(this).find('input:hidden').remove();
            $(this).attr('action','http://en.wikipedia.org/wiki/Special:Random');
        }
        else {
            $(this).submit();
        }
    });

JS Fiddle demo;

 $('form').submit(
    function(){
        if ($(this).find('input:text').val() == ''){
            $(this).find('input:hidden').remove();
            document.location.href = 'http://en.wikipedia.org/wiki/Special:Random';
            /*
                I also tried 'document.location','window.location' 
                and 'window.location.href' all of which, predictably,
                failed in exactly the same way.
            */
        }
        else {
            $(this).submit();
        }
    });

JS Fiddle demo;

Upvotes: 1

Related Questions