/    Sign up×
Community /Pin to ProfileBookmark

javacript code needed please

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?

Many thanks
Terry

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMar 18.2012 — You should start with a mapping of keywords and URLs in JavaScipt:
[CODE]var keywordMapping = {
nokia: "http://www.example.com/mobilephones.htm",
iphone: "http://www.example.com/mobilephones.htm"
};[/CODE]


Then call a function, passing in the contents of a search field:

[CODE]function checkKeyword(keyword) {
var regex, key;

for (key in keywordMapping) {
if (keywordMapping.hasOwnProperty(key)) {
regex = new RegExp("^\s*" + keywordMapping[key] + "\s*$", "i");

if (regex.test(keyword)) {
window.location = keywordMapping[key];
}
}
}
}[/CODE]


This should match things like:

"nokia"

"Nokia"

" nokiA "
Copy linkTweet thisAlerts:
@ezzo4444authorMar 18.2012 — thank you for your quick reply, its very much appreciated :-) my problem is that im limited in javascript knowledge. I copy /pasted your suggestion above and put in between <javascript> tags but nothing happened :-( Sorry for being such a noob

erm could you put the script so i can just copr / paste it into my webpage and i can manipulate it from there please :o
Copy linkTweet thisAlerts:
@toicontienMar 18.2012 — <script type="text/javascript">

// script I wrote above

</script>

Now, your next step is attaching the proper event handlers so you can process the keywords. Can you post the HTML you have?
Copy linkTweet thisAlerts:
@ezzo4444authorMar 18.2012 — :-/ ???! erm i just have a blank page at the moment as i want to add the search box first.
Copy linkTweet thisAlerts:
@toicontienMar 18.2012 — How much do you know about HTML? You'll want to start by creating a <form>. Inside of that, you'll want <input type="text" name="keywords">. Let's start with the HTML first. Can you create the form with a single text field?
Copy linkTweet thisAlerts:
@ezzo4444authorMar 18.2012 — Hello. i have created a form using emailmeform.com . I hope this helps :-/


<form id="emf-form" enctype="multipart/form-data" method="post" action="http://www.emailmeform.com/builder/form/0v72krw30DlI0" name="emf-form">

<table style="text-align:left;" cellpadding="2" cellspacing="0" border="0" bgcolor="#FFFFFF">

<tr>

<td style="" colspan="2">

<font face="Verdana" size="2" color="#000000"><b style="font-size:20px;">form</b><br />

<br /></font>

</td>

</tr>

<tr valign="top">

<td id="td_element_label_0" style="" align="left">

<font face="Verdana" size="2" color="#000000"><b>Single Line Text</b></font>

</td>

<td id="td_element_field_0" style="">

<input id="element_0" name="element_0" value="" size="30" class="validate[optional]" type="text" />

<div style="padding-bottom:8px;color:#000000;"></div>

</td>

</tr>

<tr>

<td colspan="2" align="left">

<input name="element_counts" value="1" type="hidden" /> <input name="embed" value="forms" type="hidden" /><input value="Send email" type="submit" />

</td>

</tr>

</table>

</form>
Copy linkTweet thisAlerts:
@toicontienMar 19.2012 — Ok, great. Now, the next step is to remove all the unneeded HTML. Let's strip it down to the bare essentials:

1) The <form>

2) The <input>

3) The <button>
Copy linkTweet thisAlerts:
@ezzo4444authorMar 19.2012 — erm im not entirley sure how to do that but heres my best shot
Copy linkTweet thisAlerts:
@ezzo4444authorMar 19.2012 — I have no idea how to do that :-( Could you not put a script together for me and I will pay you for your time?
×

Success!

Help @ezzo4444 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...