4

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>

asked Aug 10, 2013 at 23:19
3
  • 2
    Using the same identifier for function name and variable name may not be a very good idea. Commented Aug 10, 2013 at 23:21
  • You should do a little more research/homework. 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. Commented Aug 10, 2013 at 23:25

3 Answers 3

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 completely into your JavaScript code, and remove it in the HTML:

document.getElementById('link').onclick = function()
{
 location.href = document.getElementById('link_id').value;
};
answered Aug 10, 2013 at 23:29
Sign up to request clarification or add additional context in comments.

5 Comments

This is really good, i'l take a further look into it, thank you!
The "onclick" event does not require "javascript:" inside. Also remember to add "return false" to avoid a form being submitted. <input type="button" id="link" value="Search" onclick="goTo(); return false;"> or, even better, in the FORM HTML tag, add the "onsubmit" event with return false <form action="..." method="..." onsubmit="return false;">.
@AlejandroIván A lot of times if you are trying to access other JavaScript variables you want your function to execute in the global context. Also, his button is not type="submit" and isn't inside of form tags so it will not submit anything.
Is there a way where I don't have to write 'http://', and just the domain name by itself?
change it to be location.href = 'http://' + document.getElementById('link_id').value;
2

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.com/#q='+g1 +" site:linkedin.com", '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.

answered Oct 10, 2015 at 7:20

Comments

2

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>
answered Aug 10, 2013 at 23:27

1 Comment

Thank you! And thanks to all the comments, I know what I did wrong now!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.