Not redirecting

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Not redirecting

Post by Nay »

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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

use document.location

not

window.location

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

it might be better to do it this way

Code: Select all

<script language="JavaScript"> 
<!-- 

var location = prompt("Which function?", "Enter here");
if(location) &#123;
	document.location = "http://php.net/" + location;
&#125;

//--> 
</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 »

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") &#123;
document.location = "http://php.net/";
&#125; else &#123;
document.location = new_location;
&#125;

//-->
</script>
So I don't have to manually type anymore :D.

-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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

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) &#123; 
document.location = new_location; 
&#125; else &#123; 
document.location = "http://php.net/";
&#125; 

//--> 
</script>
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

this works, cos if you press cancell NULL is retured the the if is false.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

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 »

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

&lt;script language="JavaScript"&gt;
&lt;!--

var location = prompt("Which function?", "Enter here");
var new_location = "parse.php?function=" + location;
document.location = new_location;

//--&gt;
&lt;/script&gt;
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 :lol:.

-Nay
Post Reply