URL and Javascript
3 posts
• Page 1 of 1
URL and Javascript
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
Re: URL and Javascript
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>
3 posts
• Page 1 of 1
Return to HTML, CSS, and Scripts
Who is online
Users browsing this forum: No registered users and 6 guests