A Digital Age Deserves A Digital Leader

URL and Javascript

URL and Javascript

Postby iandotcom » Mon Nov 27, 2006 9:56 pm

Hey there,

Does anyone know a javascript that will write a textbox that the user will be able to type into, and will add to a URL and go to it.

What I mean is that if the script is set to http://www.somewebsite.com/ and the user types into the box something and clicks Go or something similar, then the user will be taken to http://www.somewebsite.com/something.

Anyone have any templates or code I can work from?

Cheers,
From, iandotcom :byebye:
Image
PRO Level 3
Posts: 85
Joined: Sun Aug 03, 2003 6:09 pm
Location: England

Re: URL and Javascript

Postby weazzle » Sat Dec 02, 2006 3:26 pm

iandotcom wrote:Hey there,

Does anyone know a javascript that will write a textbox that the user will be able to type into, and will add to a URL and go to it.

What I mean is that if the script is set to http://www.somewebsite.com/ and the user types into the box something and clicks Go or something similar, then the user will be taken to http://www.somewebsite.com/something.

Anyone have any templates or code I can work from?

Cheers,


Add the textbox and button to your code body:
Code: Select all
<body>
<!-- other content -->
http://oregonstate.edu/<input type="text" id="urlBox" value="">
<br>
<input type="button" id="urlButton" value="Go To" onclick="newPage()">
<!-- other content -->


You need an id for both the button and the input text box so you can reference them via javascript. You will write your own javascript function, in this case it is called newPage(), and it will provide the functionality you requested.

Now you can add your javascript code. This can either be placed in a .js file and included in the <head> section of your page, or written directly in to the <head> section of the page, like this example:
Code: Select all
<head>
<!-- Your other head stuff -->
<script style="text/javascript">
    function newPage() {
        var theURL = new String("http://oregonstate.edu/")
        var newURL = theURL + document.getElementById('urlBox').value;
   document.location.href = newURL;
    }
</script>
</head>
Image
PRO Level 12
User avatar
Posts: 511
Joined: Thu Aug 26, 2004 4:55 pm
Location: Albuquerque, NM {USA}

Postby weazzle » Sat Dec 02, 2006 3:34 pm

Sorry, I guess examples don't work in posts. I need to get a domain so I can host examples.
Image
PRO Level 12
User avatar
Posts: 511
Joined: Thu Aug 26, 2004 4:55 pm
Location: Albuquerque, NM {USA}

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 6 guests

cron
cron