Search code examples
jqueryhtmlmenubar

How to insert Google Custom Search into Navigation Menu 'CentralizedMenu'


My first post, so forgive me if I haven't done this correctly, and thank you.

I would like to add a ‘Google Custom Search’ box to ‘Ali Shtarbanov’s menuBar /centralizedMenu’ to the right of the last item 'About' and to the left of the 3 bar icon it creates when the monitor screen is smaller - this menu will collapse / resize on smaller screens to three bars. I’m using html, no php.

When I add the Google code is just creates an empty space, no search box. This is an example of the Google functioning code I’m adding:

    <script>
    (function() {
var cx = 'xxxxxxxxxxxxxxxxxxxxxxx:xxxxxxx’;
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:search></gcse:search>

Code on his menuBar.html and the output is here: http://jsbin.com/cotewaxemi/edit?html,output And pasted in here:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>MenuBar</title>
<!-- Bootstrap Core CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS: You can use this stylesheet to override any Bootstrap styles and/or apply your own styles -->
<link href="css/custom.css" rel="stylesheet">
</head>
<body>

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Logo and responsive toggle -->
        <div class="navbar-header"> <!--This div creates a navigation button visible on smaller screens -->
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span><!--These tags create the standard 3-lin button logo on top right corner -->
                <span class="icon-bar"></span><!--Put the page less than full-screen to see this behavior -->
                <span class="icon-bar"></span>
            </button>
        </div>
        <div class="collapse navbar-collapse" id="navbar">
            <ul class="nav navbar-nav">
                <li><a href="#">Home</a></li>
                <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown1<span class="caret"></span></a><!--Requires the JavaScript files linked at the end-->
                    <ul class="dropdown-menu">
                        <li><a href="#">Item1</a></li>
                        <li><a href="#">Item2</a></li>
                        <li><a href="#">Item3</a></li>
                    </ul>
                </li>
                <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown2<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">AnotherItem</a></li>
                        <li role="separator" class="divider"></li>
                        <li class="dropdown-header">Header:</li>
                        <li><a href="#">MoreItems</a></li>
                        <li><a href="#">MoreItems</a></li>
                        <li><a href="#">MoreItems</a></li>
                    </ul>
                </li>
                <li><a href="#">Tutorials</a></li>
                <li><a href="#">About</a></li>
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>  
<!-- JavaScript is required for the dropdown menu -->
<!-- Placed at the end of the document so the pages load faster -->
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.js"></script>
<!-- Bootstrap core JavaScript-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
</body>
</html>

Thank you again, Christina P.S. I wanted to put a link into what I tried to do, but since I am a new member it stated I'm not allowed so many links.


Solution

  • Here is the working version.

    NOTE : If you can not see the whole menu, click full-page on top right of the panel.

    // Make sure in jsFiddle you have selected option onLoad
    (function() {
      var cx = '017643444788069204610:4gvhea_mvga'; // Insert your own Custom Search engine ID here
      var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
      gcse.src = (document.location.protocol == 'https' ? 'https:' : 'http:') +
          '//www.google.com/cse/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
    })();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.js"></script>
        <!-- Bootstrap core JavaScript-->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
    <!-- Navigation -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="container">
                <!-- Logo and responsive toggle -->
                <div class="navbar-header"> <!--This div creates a navigation button visible on smaller screens -->
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span><!--These tags create the standard 3-lin button logo on top right corner -->
                        <span class="icon-bar"></span><!--Put the page less than full-screen to see this behavior -->
                        <span class="icon-bar"></span>
                    </button>
                </div>
                <div class="collapse navbar-collapse" id="navbar">
                    <ul class="nav navbar-nav">
                        <li><a href="#">Home</a></li>
                        <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown1<span class="caret"></span></a><!--Requires the JavaScript files linked at the end-->
                            <ul class="dropdown-menu">
                                <li><a href="#">Item1</a></li>
                                <li><a href="#">Item2</a></li>
                                <li><a href="#">Item3</a></li>
                            </ul>
                        </li>
                        <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown2<span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">AnotherItem</a></li>
                                <li role="separator" class="divider"></li>
                                <li class="dropdown-header">Header:</li>
                                <li><a href="#">MoreItems</a></li>
                                <li><a href="#">MoreItems</a></li>
                                <li><a href="#">MoreItems</a></li>
                            </ul>
                        </li>
                        <li><a href="#">Tutorials</a></li>
                        <li><a href="#">About</a></li>
                      <li id="gsrc"><a data-toggle="modal" data-target="#myModal">
                        <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                        </a></li>
                      <li>
                    </ul>
                </div>
                <!-- /.navbar-collapse -->
            </div>
            <!-- /.container -->
        </nav>	
        <!-- JavaScript is required for the dropdown menu -->
        <!-- Placed at the end of the document so the pages load faster -->
        <!-- jQuery -->
    
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">
    
        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Google Search</h4>
          </div>
          <div class="modal-body">
            <gcse:search></gcse:search>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
    
      </div>
    </div>