nsForum logo

Welcome Guest ( Log In )

> How To: Create Custom Search Controls, ... with your own HTML
AndyT - MC
post Mar 18 2009, 05:28 PM
Post #1





Group: Verified NS Member
Posts: 979
Joined: 22-October 07
From: St. Louis, MO
Member No.: 170



Often times I am asked how to add a search box to the header of the storefront. Most of the time we simply recommend the nsScript function:

CODE
<ns:Search type="custom" />

The HTML that is generated is lightweight, and can be styled fairly easily with CSS, but in some cases designers want total control over the HTML. Such was the case recently, and I prescribed a solution that allowed total control over the markup, while retaining the same search functionality that is built into the software. The search in the software simply takes the value of the textbox, encodes it for the URL, and redirects to the search page, and this is rather easy to perform using a bit of JavaScript.

First of all, you need to have at least a textbox and an input button, each with a unique ID. I used the IDs of "search-textbox" and "search-button", but you can use whatever IDs you like. I'll put these in a simple table for example purposes:

CODE
<table cellpadding="0" cellspacing="0">
  <tr>
    <td>
      <input type="textbox" id="search-textbox"/>
    </td>
    <td>
      <input type="image" id="search-button" alt="Go" src="/images/my-search-button.gif" />
    </td>
  </tr>
</table>

Then, use this script, matching the IDs with the previous example. It can be placed in a ".js" file and linked, or simply pasted into the Top of Page HTML.

CODE
<script type="text/javascript">
  function loadSearch() {
    var searchText = document.getElementById("search-textbox");
    var searchButton = document.getElementById("search-button");

    searchText.onkeyup = function() {
      if (event.keyCode=='13') { searchButton.click();}
    };
    
    searchButton.onclick = function() {
      if (searchText.value != '') {
        window.location = '/search.aspx?find=' + encodeURIComponent(searchText.value).replace(/%20/g, '+');
        return false;
      }
    };
  }
  
  MC.addLoadEvent(loadSearch);
</script>

That's it! (IMG:http://forums.networksolutions.com/style_emoticons/default/thumbsup.gif)

If you would like the button image to change when you hover your mouse over it, use the following script instead. Set the button input "src" to your "off" image, and change the searchButtonOn/searchButtonOff variables to the matching on/off images for the button.
CODE
<script type="text/javascript">
  function loadSearch() {
    var searchText = document.getElementById("search-textbox");
    var searchButton = document.getElementById("search-button");

    var searchButtonOff = '/images/my-search-image-off.gif';
    var searchButtonOn  = '/images/my-search-image-on.gif';
    
    searchText.onkeyup = function() {
      if (event.keyCode=='13') { searchButton.click();}
    };
    
    searchButton.onmouseover = function() {
      this.src = searchButtonOn;
    };
    
    searchButton.onmouseout = function() {
      this.src = searchButtonOff;
    };
    
    searchButton.onclick = function() {
      if (searchText.value != '') {
        window.location = '/search.aspx?find=' + encodeURIComponent(searchText.value).replace(/%20/g, '+');
        return false;
      }
    };
  }
  
  MC.addLoadEvent(loadSearch);
</script>
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies (1 - 15)
LittleThings
post May 1 2009, 11:27 AM
Post #2





Group: Verified NS Member
Posts: 14
Joined: 26-March 09
Member No.: 4,068



This appears to be broken with the 7.6 release. My search (utilizing the code above) no longer works. Could it be the MC.addLoadEvent changed?
Any help would be appreciated.

My site (currently under development) is here: http://02d3fdf.netsolstores.com.
Go to the top of the page
 
+Quote Post
AndyT - MC
post May 1 2009, 01:05 PM
Post #3





Group: Verified NS Member
Posts: 979
Joined: 22-October 07
From: St. Louis, MO
Member No.: 170



Your search is working fine for me.
Go to the top of the page
 
+Quote Post
LittleThings
post May 1 2009, 08:37 PM
Post #4





Group: Verified NS Member
Posts: 14
Joined: 26-March 09
Member No.: 4,068



QUOTE (AndyT - MC @ May 1 2009, 02:15 PM) *
Your search is working fine for me.


That is so weird. I have tried it from multiple machines and it just reloads the main page for me. Oh wait. I just tried it in FireFox 2.0 vs. IE 7. It works in Firefox but not IE7. I would ask, is that a javascript issue but my menus (which are javascript) work just fine.

Any ideas?

As an addendum: I discovered it was a problem with my menu script not using a "return false;" where it should and thus causing the page to reload in IE7. Apparently Firefox processes all of the client script before calling back to the server and IE7 does both simultaneously.
Go to the top of the page
 
+Quote Post
jjjusme
post May 12 2009, 03:04 PM
Post #5





Group: Verified NS Member
Posts: 4
Joined: 10-December 08
Member No.: 3,285



Can this method be used on a page that is outside of the e-commerce system? For instance, our home page is not generated by e-commerce, it is a static page housed on the NS host server.

Any way to add the search to these external pages?

How about a mini-cart where item total is shown?

Thank you for your help!
Go to the top of the page
 
+Quote Post
AndyT - MC
post May 12 2009, 03:32 PM
Post #6





Group: Verified NS Member
Posts: 979
Joined: 22-October 07
From: St. Louis, MO
Member No.: 170



QUOTE (jjjusme @ May 12 2009, 03:14 PM) *
Can this method be used on a page that is outside of the e-commerce system? For instance, our home page is not generated by e-commerce, it is a static page housed on the NS host server.

Any way to add the search to these external pages?

Sure, just put the full domain in the "window.location" section. ex:
window.location = 'http://www.mydomain.com/search.aspx?find....

QUOTE (jjjusme @ May 12 2009, 03:14 PM) *
How about a mini-cart where item total is shown?

This is not possible at this time.
Go to the top of the page
 
+Quote Post
jjjusme
post May 13 2009, 05:35 PM
Post #7





Group: Verified NS Member
Posts: 4
Joined: 10-December 08
Member No.: 3,285



QUOTE (AndyT - MC @ May 12 2009, 04:42 PM) *
Sure, just put the full domain in the "window.location" section. ex:
window.location = 'http://www.mydomain.com/search.aspx?find....


I still cannot get this to work. Can you tell me what I am missing?

http://www.andyandmeg.com/bornfit/index.html

Thank you!!
Go to the top of the page
 
+Quote Post
AndyT - MC
post May 13 2009, 05:41 PM
Post #8





Group: Verified NS Member
Posts: 979
Joined: 22-October 07
From: St. Louis, MO
Member No.: 170



2 things ....

- you gave your search textbox the id "search-button", it needs to have the id "search-textbox"

- "MC.addLoadEvent(loadSearch)" will only work on the site, since it is part of the cart software javascript.

You can fix this by changing this line from:
MC.addLoadEvent(loadSearch);

To:
window.onload = loadSearch;
Go to the top of the page
 
+Quote Post
jjjusme
post May 13 2009, 05:59 PM
Post #9





Group: Verified NS Member
Posts: 4
Joined: 10-December 08
Member No.: 3,285



QUOTE (AndyT - MC @ May 13 2009, 06:51 PM) *
2 things ....

- you gave your search textbox the id "search-button", it needs to have the id "search-textbox"

- "MC.addLoadEvent(loadSearch)" will only work on the site, since it is part of the cart software javascript.

You can fix this by changing this line from:
MC.addLoadEvent(loadSearch);

To:
window.onload = loadSearch;


Duh. Should have seen the naming issue. Thanks for your help. Works great now!!!!
Go to the top of the page
 
+Quote Post
AnneAshley
post Aug 19 2009, 03:46 PM
Post #10





Group: Verified NS Member
Posts: 12
Joined: 9-June 09
Member No.: 4,570



I have added a jump menu to allow folks to sear by one of my assigned attributes. It works, but in IE8 there is a glich. It creates a gap. When I removed the form code, the gap disappears. Is there a fix? I really want to use the jump menu.
Go to the top of the page
 
+Quote Post
AndyT - MC
post Aug 19 2009, 04:09 PM
Post #11





Group: Verified NS Member
Posts: 979
Joined: 22-October 07
From: St. Louis, MO
Member No.: 170



<form> tags cannot be used in the software, since it results in nested forms, and that is illegal in HTML. If you could provide a link I will take a look.

(by the way, since this is not related to this particular how-to, I will likely split it into a separate post)
Go to the top of the page
 
+Quote Post
AnneAshley
post Aug 20 2009, 11:09 AM
Post #12





Group: Verified NS Member
Posts: 12
Joined: 9-June 09
Member No.: 4,570



QUOTE (AndyT - MC @ Aug 19 2009, 05:09 PM) *
<form> tags cannot be used in the software, since it results in nested forms, and that is illegal in HTML. If you could provide a link I will take a look.

(by the way, since this is not related to this particular how-to, I will likely split it into a separate post)


Here is a link to the form.
http://chown.com/Files/netsol/c2cmenu.htm
Go to the top of the page
 
+Quote Post
AndyT - MC
post Aug 20 2009, 11:13 AM
Post #13





Group: Verified NS Member
Posts: 979
Joined: 22-October 07
From: St. Louis, MO
Member No.: 170



I believe that will work just fine if you completely remove the surrounding <form> completely, since it works with javascript.
Go to the top of the page
 
+Quote Post
AnneAshley
post Aug 20 2009, 11:41 AM
Post #14





Group: Verified NS Member
Posts: 12
Joined: 9-June 09
Member No.: 4,570



Thank you, it works great.
Go to the top of the page
 
+Quote Post
den2009
post Aug 23 2009, 08:37 PM
Post #15





Group: Verified NS Member
Posts: 10
Joined: 2-April 09
Member No.: 4,107



My search no longer works (just reloads whatever page it is on) and not sure when this stopped. I just put in the html again suggested above (was already there for the id'ing but did not see the rest, so put it in at end). Ditto for the upper left hand logo on every page -- used to be able to click on it to get back to main home page. Now I can't seem to get the hyperlink to work at all. Could someone please view my source and tell me what the problem is? Now I am worried that other things might not be working right since one of these upgrades. I never check. Anyway, any help MUCH MUCH MUCH appreciated as both are important functions.

www.chaiseandlounge.com
Go to the top of the page
 
+Quote Post
AndyT - MC
post Aug 24 2009, 08:16 AM
Post #16





Group: Verified NS Member
Posts: 979
Joined: 22-October 07
From: St. Louis, MO
Member No.: 170



QUOTE (den2009 @ Aug 23 2009, 08:37 PM) *
My search no longer works (just reloads whatever page it is on) and not sure when this stopped. I just put in the html again suggested above (was already there for the id'ing but did not see the rest, so put it in at end). Ditto for the upper left hand logo on every page -- used to be able to click on it to get back to main home page. Now I can't seem to get the hyperlink to work at all. Could someone please view my source and tell me what the problem is? Now I am worried that other things might not be working right since one of these upgrades. I never check. Anyway, any help MUCH MUCH MUCH appreciated as both are important functions.

www.chaiseandlounge.com

The search works fine for me.

The logo is just an image, you need to surround it with a link element. Instead of:
<img alt="" src="/images/logoheaderleft24.gif" border="0" />

Use:
<a href="/"><img alt="" src="/images/logoheaderleft24.gif" border="0" /></a>
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
Tags
No Tag inserted yet

1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version    Network Solutions © 2009 Time is now: 21st November 2009 - 09:52 PM
Domain Names | Web Hosting | Web Design | Shopping Cart Software | Online Marketing | SSL Certificates