Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Window onlick 1

Status
Not open for further replies.

NerdTop72

Programmer
Mar 14, 2005
117
US
Hi,
I am trying to use a yahoo search box and open the search results in a new window. I'm having some difficulty understanding how to use variables between my function and my form. can some one tell me what I am doing wrong?
Thanks

Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
    <title></title>

    <script type="text/javascript">
Function SearchYahoo(txt)
{
        window.open('[URL unfurl="true"]http://search.yahoo.com/search?p=')[/URL] & txt
}

    </script>

</head>
<body>

    <!-- Yahoo! Search -->
    <form method="get" action="[URL unfurl="true"]http://search.yahoo.com/search">[/URL]
    <table cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td>
                <div style="border: 1px solid #999; padding: 5px 4px 5px 3px;">
                    <a href="[URL unfurl="true"]http://search.yahoo.com/">[/URL]
                        <img src="[URL unfurl="true"]http://us.i1.yimg.com/us.yimg.com/i/us/search/ysan/ysanlogo.gif"[/URL] align="absmiddle"
                            border="0"></a>
                    <input type="text" name="p" size="25">
                    <input type="hidden" name="fr" value="yscpb">
                    <input type="submit" value="Search" onclick="SearchYahoo(p)"></div>
            </td>
        </tr>
    </table>
    </form>
    <!-- End Yahoo! Search -->
</body>
</html>
 
give your input box an id of "p", and adjust the function slightly ...

Code:
script type="text/javascript">
Function SearchYahoo()
{
        window.open('[URL unfurl="true"]http://search.yahoo.com/search?p=')[/URL] & [b]document.getElementsById("p")[/b]
}

    </script>

<input type="text" [b]id="p"[/b] name="p" size="25">

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
 
^ sorry that should have been singular getElementById (not elements and in javascript "Function" is all lowercase function:

Code:
script type="text/javascript">
function SearchYahoo()
{
        window.open('[URL unfurl="true"]http://search.yahoo.com/search?p=')[/URL] & document.getElementById("p")
}

    </script>

<input type="text" id="p" name="p" size="25">

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
 
^ again, sorry but found another glitch -

in js you concat two strings by using the + sign, not the ampersand...there were a few other things, I actually tested the code below and I think it does what you want.

It will work if the user has javascript turned off as well.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
    <title></title>

    <script type="text/javascript">
		function SearchYahoo()
		{
			window.open('[URL unfurl="true"]http://search.yahoo.com/search?p='[/URL] + document.getElementById("p").value);
			return false;
		}
    </script>

</head>
<body>

    <!-- Yahoo! Search -->
	<form action="[URL unfurl="true"]http://search.yahoo.com/search"[/URL] target="_blank" method="get" onSubmit="return SearchYahoo()">
    <table cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td>
                <div style="border: 1px solid #999; padding: 5px 4px 5px 3px;">
                    <a href="[URL unfurl="true"]http://search.yahoo.com/">[/URL]
                        <img src="[URL unfurl="true"]http://us.i1.yimg.com/us.yimg.com/i/us/search/ysan/ysanlogo.gif"[/URL] align="absmiddle"
                            border="0"></a>
                    <input type="text" id="p" name="p" size="25">
                    <input type="hidden" name="fr" value="yscpb">
                    <input type="submit" value="Search"></div>
            </td>
        </tr>
    </table>
	</form>
    <!-- End Yahoo! Search -->
</body>
</html>



TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
 
Thanks vicvirk
can you answer another question?
How would I be able to parse variables from
Code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

is it the same as
Code:
document.getElementById("p")
instead of p would i use the ID of the textbox?
Code:
document.getElementById("Textbox1")
Thanks
 
document.getElementById("Textbox1")

^ that should work - but just to be sure, view your page in a browser then view the source and find the id that is generated by .NET - I am 99.999999% sure it will be the same as the id you specify, but check just to be sure...

Also, remember that Javascript is case sensitive.



TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top