...

View Full Version : javascript search box needed please



ezzo4444
03-18-2012, 03:10 PM
Hello everyone, i desperatly need a script for my website but am totally stuck, I hope someone can help

On my website i want a search box. I want it so that if a use types in one of my predetermined search terms then they would be sent to a predeterimed page in my own website.

For example: I have a group of 5 keywords - nokia, mobile, vodafone, cellphone, iphone. If a user types any of those keywords into my search box then xxxxx.com/mobilephones.htm will be loaded up

I have another set of 5 keywords - cat, dog, rabbit, mouse, snake. If a user types any of those into the search box then they are directed to xxxxx.com/animals.htm and so on ...

So as you can see, its not a 'normal' search engine

I will be creating new pages and will need to add the chosen keywords for it as time goes on etc

Nearly everywhere i look for something to help me all I can find are standard search engine scripts which are no good as they display search results instead of directing to specific urls depending on the keywords entered

I would also need to 'capture' what search terms are being entered so that I can build some user statistics

I understand html and javacript to an intermediate level

Can anyone help / provide a script that I could use? Although Im a student Im willing to pay some funds to anyone that can do this for me

Many thanks
Terry

Philip M
03-18-2012, 05:24 PM
This ought to move you forward:-


Search for <input type = "text" id = "textfield" size = "20" onblur = "go()">

<script type = "text/javascript">

function go() {
var val = document.getElementById("textfield").value.toLowerCase();
val = val.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
if ((val == "nokia") || (val == "mobile") || (val == "vodafone") || val == ("cellphone") || val == ("iphone")) {
window.location.href = "http://www.mywebsite.com/mobilephones.htm";
}
else if ((val == "cat") || (val == "dog") || (val == "rabbit") || (val == "mouse") || (val == "snake") ) {
window.location.href = "http://www.mywebsite.com/animals.htm";
}
else {
alert ("That search term was not found");
return false;
}

}

</script>

Javascript is purely a client-side language and cannot capture any statistics or write information to a file on the server. You need server-side scripting for that.

"This is the worst disaster in California since I was elected." - California Governor Pat Brown, discussing a local flood

Old Pedant
03-19-2012, 02:12 AM
What do you do if the user types in "nokia phones"? Philip's code won't work, as it will only handle an input of "nokia" alone.

I'd suggest something more like this:


Search for <input type = "text" id = "textfield" size = "20" onblur = "go()">

<script type = "text/javascript">

function go()
{
var val = document.getElementById("textfield").value;
var re;
re = /(nokia|mobile|vodafone|cellphone|iphone)/i;
if ( re.test(val) )
{
window.location.href = "http://www.mywebsite.com/mobilephones.htm";
}
re = /(cat|dog|rabbit|mouse|snake)/i;
if ( re.test(val) )
{
window.location.href = "http://www.mywebsite.com/animals.htm";
}
alert ("That search term was not found");
return false;
}
</script>

Note: the /i at the end of each regular expression makes the match case-insensitive. So "nokia" and "NOKIA" and "Nokia" and "nOkiA" will all match.

Note: you don't need else in the code as changing the location.href is immediate and effectively terminates the JavaScript.

webdev1958
03-19-2012, 08:30 AM
Hello everyone, i desperatly need a script for my website but am totally stuck, I hope someone can help



Normally this would all be done server side with a database and not javascript. For one thing, not all portable devices even support javascript.

Doing all this server side means you can store the keywords, urls and statistics in a database which will make maintenace of your website a whole lot easier. You could use an open source CMS or customise your own.

So unless you must do this with javascript, I would recommend you ditch that idea and do it server side with a database.

Philip M
03-19-2012, 08:30 AM
The only trouble with using a regex is that you must be careful to avoid problems with Penistone and Scunthorpe. :D Best to use \b (word boundary) to match whole words only.

webdev1958/bullant offers the same advice as the Irishman who was asked for the best way to get to Dublin, and replied that if it would be easiet to not start from here! :D Quite right, and totally unhelpful.

webdev1958
03-19-2012, 08:41 AM
webdev1958/bullant offers the same advice as the Irishman who was asked for the best way to get to Dublin, and replied that if it would be easiet to not start form here! :D

I'm just saying I wouldn't waste my time doing this with javascript because of the reasons I posted :thumbsup:, The op might not be aware a server side option even exists.

Can you post anything that proves anything I posted is not true? :)

Philip M
03-19-2012, 08:44 AM
I'm just saying I wouldn't waste my time doing this with javascript because of the reasons I posted :thumbsup:

Can you post anything that proves anything I posted is not true? :)

I did not say it was untrue. I said it was quite right, but unhelpful.

webdev1958
03-19-2012, 08:45 AM
I did not say it was untrue. I said it was quite right, but unhelpful.

Maybe to you ;) but the op can decide for him/her self. I doubt they need you to decide for them :thumbsup:

As long as the op is aware of a server side option and its advantages, then my work is done. What they eventually decide to do means zip to me :)

Philip M
03-19-2012, 08:53 AM
Maybe to you ;) but the op can decide for him/her self. I doubt they need you to decide for them :thumbsup:

Agreed! They can identify bull**** (or bullant****) themselves without my help. :D

webdev1958
03-19-2012, 09:00 AM
webdev1958/bullant offers ......
Agreed! They can identify bull**** (or bullant****) themselves without my help. :D

You keep repeating your confusion between me and someone else so all I can do is repeat my reply :)



yeah whatever :thumbsup: :)

You clearly still have me confused with someone else.

I don't know when this bullant character was supposedly banned, but I've been here 11 months and made nearly 400 posts.

Clearly, the owners of codingforums either don't agree with you or they are happy for members to come back under a new name. It has to be one or the other, you can't have it both ways after 11 months (and 399 posts) of me being here :)

So clearly, either you are totally wrong or members are allowed to come back under new names :)

Philip M
03-19-2012, 09:07 AM
So clearly, either you are totally wrong or members are allowed to come back under new names

I am completely baffled why people who have been banned are allowed to rejoin the forum under a new name. It is a total mystery to me.

ezzo4444
03-20-2012, 06:00 PM
Normally this would all be done server side with a database and not javascript. For one thing, not all portable devices even support javascript.

Doing all this server side means you can store the keywords, urls and statistics in a database which will make maintenace of your website a whole lot easier. You could use an open source CMS or customise your own.

So unless you must do this with javascript, I would recommend you ditch that idea and do it server side with a database.

thanks everyone for all your help, i guess the server-side option would be the best , long term option. I only know basic html and all the search engine scripts don't do what im looking for. Can anyone recommend a ready made package? Can anyone develop this for me and install onto my server. I do have some funds available for a programmer?

Philip M
03-20-2012, 06:18 PM
thanks everyone for all your help, i guess the server-side option would be the best , long term option. I only know basic html and all the search engine scripts don't do what im looking for. Can anyone recommend a ready made package? Can anyone develop this for me and install onto my server. I do have some funds available for a programmer?

Post in the Web Projects section of the forum.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum