Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    javascript search box 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? Although Im a student Im willing to pay some funds to anyone that can do this for me

    Many thanks
    Terry

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    This ought to move you forward:-
    Code:
    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
    Last edited by Philip M; 03-18-2012 at 05:30 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    ezzo4444 (03-20-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,548
    Thanks
    77
    Thanked 4,382 Times in 4,347 Posts
    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:
    Code:
    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.
    Last edited by Old Pedant; 03-19-2012 at 02:14 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    ezzo4444 (03-20-2012)

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by ezzo4444 View Post
    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.

  • Users who have thanked webdev1958 for this post:

    ezzo4444 (03-20-2012)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    The only trouble with using a regex is that you must be careful to avoid problems with Penistone and Scunthorpe. 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! Quite right, and totally unhelpful.
    Last edited by Philip M; 03-19-2012 at 08:42 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    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!
    I'm just saying I wouldn't waste my time doing this with javascript because of the reasons I posted , The op might not be aware a server side option even exists.

    Can you post anything that proves anything I posted is not true?
    Last edited by webdev1958; 03-19-2012 at 08:44 AM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by webdev1958 View Post
    I'm just saying I wouldn't waste my time doing this with javascript because of the reasons I posted

    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    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

    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
    Last edited by webdev1958; 03-19-2012 at 08:53 AM.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by webdev1958 View Post
    Maybe to you but the op can decide for him/her self. I doubt they need you to decide for them
    Agreed! They can identify bull**** (or bullant****) themselves without my help.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [ot]
    Quote Originally Posted by Philip M View Post
    webdev1958/bullant offers ......
    Quote Originally Posted by Philip M View Post
    Agreed! They can identify bull**** (or bullant****) themselves without my help.
    You keep repeating your confusion between me and someone else so all I can do is repeat my reply


    yeah whatever

    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
    [/ot]
    Last edited by webdev1958; 03-19-2012 at 09:08 AM.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by webdev1958 View Post
    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?

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by ezzo4444 View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •