PDA

View Full Version : window.location not working



sunny
05-13-2006, 03:54 PM
here is my code,


<html><body>
<form name="jumpurl1" onSubmit="return jumpit()">
<input type="text" size=30 name="jumpurl2" value="http://">
<select name="jumpurl3">
<option>select</option>
<option>.com</option>
<option>.cjb.net</option></select>
<input type="button" value="Go!" onClick="jumpit()">
</form>
<script>
function jumpit(){
window.location=document.jumpurl1.jumpurl2.jumpurl3.value
return false
}
</script>
</body></html>

this script works when jumpurl3 is absent from window.location statement.but in it's present it doesn't work. Can any one tell why?

Twey
05-13-2006, 04:08 PM
Because document.jumpurl1.jumpurl2.jumpurl3 doesn't exist.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>A title</title>
</head>
<body>
<form action="?" onsubmit="
window.location.href =
this.elements['jumpurl2'].value +
this.elements['jumpurl3'].value;
">
<p>
<input type="text" size="30" name="jumpurl2" value="http://..." style="
color: silver;
background-color: white;
"
onfocus="
with(this) {
if(!first) {
value = '';
style.color='black';
first = true;
}
}
">
<select name="jumpurl3">
<option value="">select</option>
<option>.com</option>
<option>.cjb.net</option>
</select>
<input type="submit" value="Go!">
</p>
</form>
</body>
</html>

sunny
05-20-2006, 03:17 PM
well,
The outcome of the script which you gave was:
file:///C:/WINDOWS/Desktop/123.html?jumpurl2=http%3A%2F%2Fwww.yahoo&jumpurl3=.com
when i typed yahoo and selected .com as option.

Twey
05-20-2006, 03:20 PM
OK, try this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>A title</title>
</head>
<body>
<form action="?" onsubmit="
window.location.href =
"http://" +
this.elements['jumpurl2'].value +
this.elements['jumpurl3'].value;
">
<p>
<input type="text" size="30" name="jumpurl2" value="http://..." style="
color: silver;
background-color: white;
"
onfocus="
with(this) {
if(!first) {
value = '';
style.color='black';
first = true;
}
}
">
<select name="jumpurl3">
<option value="">select</option>
<option>.com</option>
<option>.cjb.net</option>
</select>
<input type="submit" value="Go!">
</p>
</form>
</body>
</html>Oh, and it ought to be noted that cjb.net isn't a top-level domain :)

cybercoder
05-20-2006, 04:45 PM
Oh, and it ought to be noted that cjb.net isn't a top-level domain :)

Yeh, it is a redirector, but in the form of a sub domain.

sunny
05-23-2006, 03:12 PM
Still the same output.Anyways,thanks for your replies

killerbean4u
02-06-2008, 09:52 AM
<html><body>
<form name="jumpurl1" onSubmit="return jumpit()">
<input type="text" size=30 name="jumpurl2" value="http://">
<select name="jumpurl3">
<option value="">select</option>
<option value=".com">.com</option>
<option value=".cjb.net">.cjb.net</option></select>
<input type="button" value="Go!" onClick="jumpit()">
</form>
<script>
function jumpit(){
window.location=document.jumpurl1.jumpurl2.value + document.jumpurl1.jumpurl3.value
return false
}
</script>
</body></html>




Your code had two problems
1) jumpurl3 is not a child of jumpurl2
2) you didnt put values in options


I know its too late but may help someone else :)

Twey
02-06-2008, 06:50 PM
This thread is pretty ancient. Here's how I'd write it now:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>A title</title>
</head>
<body>
<form action="jumpurl.cgi" onsubmit="
var e = this.elements,
u = e.urlstring.value,
d = e.urldom.value;
return !!(u && d
&& (u.defaultValue !== u.value)
&& (this.action = u + d));
">
<p>
<input type="text" name="urlstring" value="http://..." style="
color: silver;
background-color: white;
"
onfocus="
this.value === this.defaultValue && (this.value = '');
this.style.color = 'black';
"
onblur="
if(!this.value) {
this.value = this.defaultValue;
this.style.color = 'silver';
}
">
<select name="urldom">
<option value="">(select one)</option>
<option value=".com">.com</option>
<option value=".cjb.net">.cjb.net</option>
</select>
<input type="submit" value="Go!">
</p>
</form>
</body>
</html>... where "jumpurl.cgi" should be a server-side script capable of taking the form values and redirecting the user to the appropriate page, without Javascript.
<html><body>Your document is missing a DOCTYPE and its <head> tag.
<form name="jumpurl1" onSubmit="return jumpit()">
<input type="text" size=30 name="jumpurl2" value="http://">A block-level element is required here. Also, the name attribute is deprecated for this purpose.
<script>The <script> element requires a type attribute.
window.location=document.jumpurl1.jumpurl2.value + document.jumpurl1.jumpurl3.valueAccessing form elements this way is deprecated and may cause problems depending on the element names (you should use the forms and elements collections). You also (as a matter of good coding style) should try to use semicolons to unambiguously separate one statement from the next. The Javascript parser, if there are no semicolons, will attempt to form the longest statement possible, and this isn't always what you'd expect. Consider:
var a = 2
(new Foo()).bar()This will throw an error "2 is not a function," because the interpreter will understand it as:
var a = 2(new Foo()).bar();rather than the (probably expected)
var a = 2;
(new Foo()).bar();
Your code had two problems
1) jumpurl3 is not a child of jumpurl2How is that a problem? There's no way one <input> can be the child of another :-\