最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Simple JavaScript search box - Stack Overflow

programmeradmin4浏览0评论

I tried connecting the first text box, so it would turn it into a URL, and then when 'search' is clicked, it would jump to that website, not sure if it's impossible, or i'm just clueless, but would really appreciate some help, thank you in advance!

<html>

<head>
</head>

<body>
  <script type="text/javascript">
    function link() {
      var link_s = document.getElementById('link_id').value;
      document.getElementById('link_str').innerHTML = link_s.link()
    }
  </script>

  <h2> Search box </h2>
  <input type='text' id='link_id'>
  <input type='button' id='link_str' value='Search' onClick='link()'>
</body>
<html>

I tried connecting the first text box, so it would turn it into a URL, and then when 'search' is clicked, it would jump to that website, not sure if it's impossible, or i'm just clueless, but would really appreciate some help, thank you in advance!

<html>

<head>
</head>

<body>
  <script type="text/javascript">
    function link() {
      var link_s = document.getElementById('link_id').value;
      document.getElementById('link_str').innerHTML = link_s.link()
    }
  </script>

  <h2> Search box </h2>
  <input type='text' id='link_id'>
  <input type='button' id='link_str' value='Search' onClick='link()'>
</body>
<html>

Share Improve this question edited Mar 3, 2021 at 8:46 SK-the-Learner 5435 silver badges20 bronze badges asked Aug 10, 2013 at 23:19 SamirSamir 3,1974 gold badges19 silver badges19 bronze badges 3
  • 2 Using the same identifier for function name and variable name may not be a very good idea. – Dimitri Vorontzov Commented Aug 10, 2013 at 23:21
  • You should do a little more research/homework. – user2625787 Commented Aug 10, 2013 at 23:22
  • 1 Well, what is str and where is it defined? You've either left that out of your snippet or are trying to use an undeclared variable. – Jonathan Lonowski Commented Aug 10, 2013 at 23:25
Add a ment  | 

3 Answers 3

Reset to default 3

Try this JavaScript:

function goTo()
{
    location.href = document.getElementById('link_id').value;
}

and change the onclick in the HTML:

<input type='text' id='link_id'>
<input type='button' id='link' value='Search' onClick='javascript:goTo()'>

Edit:

If you want to follow the unobtrusive JavaScript way, you would move the onclick pletely into your JavaScript code, and remove it in the HTML:

document.getElementById('link').onclick = function()
{
    location.href = document.getElementById('link_id').value;
};

if you want to create a form that will search google use this:

<script type="text/javascript">
function dos12(g1) {
window.open('https://www.google./#q='+g1 +" site:linkedin.",      'G1window');
}

      </script>
  <form onsubmit="dos12(this.g1.value); return false;">
    <input type="text" name="g1" size="20" placeholder="Name" />
            <input type="submit" value="Submit" />
    Search Linkedin via Google<br />
    <br />
  </form>

If you want to search a website then you will need to get the search string used. for example, if you want to search the ABN lookup site for Australia you would use the following code.

<script type="text/javascript">
function dos10(a1) {
window.open('http://abr.business.gov.au/SearchByName.aspx?SearchText=' + a1,   'a1window');
}

      </script>
  <form onsubmit="dos10(this.a1.value); return false;">
    <input type="text" name="a1" size="20" placeholder="Name" />
            <input type="submit" value="Submit" />
    ABN Lookup name<br />
    <br />
  </form>

hope this helps. you don't need to add anything else. Just copy and paste this into notepad or your code editor and save as test.html then open with browser to test it.

Here's how I would do it:

 <input type="text" id="link-box"/>
 <input type="button" id="search-button" value="Search" 
        onclick="window.location = document.getElementById('link-box').value;"/>

Of course you could do this:

 <script type="text/javascript">
      function func(){
           window.location = document.getElementById('link-box').value;
      }
 </script>

With onclick="func();"

Or

<script type="text/javascript">
       document.getElementById("search-button").onclick = function(){
            window.location = document.getElementById('link-box').value;  
       };        
 </script>

Or last of all

 <script type="text/javascript">
       document.getElementById("search-button").addEventListener("click", function(){
            window.location = document.getElementById('link-box').value;
       });
 </script>
发布评论

评论列表(0)

  1. 暂无评论