JavaScript and client side scripting.
Moderator: General Moderators
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Fri Oct 24, 2003 8:17 am
Hey, I was thinking, I'm going to have this page saved in my hard drive and have it as a link in the Links bar. Well, it's supposed to prompt me and It'll redirect me to the page I want, at PHP.net. Here it goes:
Code: Select all
<script language="JavaScript">
<!--
var location = prompt("Which function?", "Enter here");
var new_location = "http://php.net/" + location;
window.location = new_location;
//-->
</script>
Well, I get the prompt but after that, it doesn't go anywhere. Any ideas?
-Nay
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Oct 24, 2003 8:49 am
use document.location
not
window.location
Mark
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Oct 24, 2003 8:51 am
it might be better to do it this way
Code: Select all
<script language="JavaScript">
<!--
var location = prompt("Which function?", "Enter here");
if(location) {
document.location = "http://php.net/" + location;
}
//-->
</script>
because, the way you did it, the user will still be redirected if the click cancel. This way, you can put an else statement in a do something if the click cancel.
Mark
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Fri Oct 24, 2003 8:52 am
woohoo thanks Bech. Now it's working:
Code: Select all
<script language="JavaScript">
<!--
var location = prompt("Which function?", "Enter here");
var new_location = "http://php.net/" + location;
if(new_location=="null") {
document.location = "http://php.net/";
} else {
document.location = new_location;
}
//-->
</script>
So I don't have to manually type anymore
.
-Nay
edit: I don't know about anyone, but I'd like to be redirected to php.net if I did not enter a function. So I can have a 2-in-1 kind of a link.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Oct 24, 2003 9:02 am
The way you have done it isn't wrong, but i think this would be more correct in the code world.
Code: Select all
<script language="JavaScript">
<!--
var location = prompt("Which function?", "Enter here");
var new_location = "http://php.net/" + location;
if(new_location) {
document.location = new_location;
} else {
document.location = "http://php.net/";
}
//-->
</script>
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Fri Oct 24, 2003 9:06 am
hehe, yeah, thanks. I'm not much into Javascript. I was wondering if there was an isSet() for Javascript, then I couldn't find any so I did it that way lol.
-Nay
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Oct 24, 2003 9:12 am
this works, cos if you press cancell NULL is retured the the if is false.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Oct 24, 2003 9:14 am
you could aslo just add this link to your favourites which will do the same thing
Code: Select all
javascript:q=document.selection.createRange().text;if(!q)void(q=prompt('PHP%20Reference:',''));if(q)location.href='http://se.php.net/manual-lookup.php?function='+escape(q)
Works a treat
Mark
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Fri Oct 24, 2003 9:33 am
Okay, nevermind it all now. I got fustrated with JavaScript since I still get redirected to "
http://www.php.net/null ". So I did this:
locahost/phpnet/index.html:
Code: Select all
<script language="JavaScript">
<!--
var location = prompt("Which function?", "Enter here");
var new_location = "parse.php?function=" + location;
document.location = new_location;
//-->
</script>
localhost/phpnet/parse.php:
Code: Select all
<?php
$function = $_GET['function'];
if(isSet($function) && $function != "null") {
$location = "http://www.php.net/" . $function;
header("Location: $location");
} else {
header("Location: http://www.php.net/");
}
?>
There! I love it better now, since there's some PHP in it
.
-Nay