Im trying to make a simple form … where you can type a URL into the input box and when you click the button. It will take you to the URL that you typed.
Its actually pretty simple to check for http etc, simply use the code below and it will work fine.
<form action="" method="get" id="links">
<input type="text" maxlength="100" size="58" name="forum_url" />
<input type="button" value="Forums" style="width : 170px; margin: 3px" onclick="gotoURL()" />
</form>
<script>
function gotoURL() {
// Get the URL
var url = document.forms.links.forum_url.value;
// Make sure the URL has a length
if (!url.length) {
alert('Please enter a valid URL');
return false;
}
// Check if it has "http://" in the value
var regex = /^(http|ftp|https|ftps)/i;
if (!regex.test(url)) {
url = 'http://' + url;
}
window.location = url;
return false;
}
</script>
how can i make the link open in another Frame on the page? target=“framename” ?
I’m also trying to figure out a way for the form values to “remember me”… I know this can be done through cookies but all the methods I have tried so far have been faliures.
Below is the code that will allow you to open the link in an iframe
<form action="" method="get" id="links">
<input type="text" maxlength="100" size="58" name="forum_url" />
<input type="button" value="Forums" style="width : 170px; margin: 3px" onclick="gotoURL()" />
</form>
<script>
function gotoURL() {
// Get the URL
var url = document.forms.links.forum_url.value;
// Make sure the URL has a length
if (!url.length) {
alert('Please enter a valid URL');
return false;
}
// Check if it has "http://" in the value
var regex = /^(http(s)?|ftp(s)?)/i;
if (!regex.test(url)) {
url = 'http://' + url;
}
// Create a new iframe
createIframe('urlFromForm', url);
return false;
}
function createIframe(iframe, url) {
// Check if any iframe elements exists on the page
//
// If one exists that has the exact same name then skip the
// DOM build process
var iframes = document.getElementsByTagName('iframe'), ele = false, exists = false;
if (iframes.length) {
for(var i = 0; i < iframes.length; i++) {
if (iframes[i].name == iframe) {
exists = true, ele = iframes[i];
break;
}
}
}
if (!exists) {
// Create the new iframe element
ele = document.createElement('iframe');
// Set the iframe attributes
ele.name = iframe;
ele.width = '500px';
ele.height = '500px';
ele.frameborder = 'none';
ele.style.border = 'none';
ele.style.allowtransparency = 'true';
ele.style.margin = '0px';
ele.scrolling = 'yes';
// Append the iframe to the body
document.body.appendChild(ele);
}
ele.src = url;
}
</script>
As for the cookie remember what is the code you have tried, could you please post it.