PDA

View Full Version : redirect url with javascript



kamkam
Sep 15th, 2007, 10:19 AM
Hello;
I got a problem with using redirect url with javascript. Could anyone help, please.



<html>
<head>
<script type="text/javascript">
function myFuction(char){
setTimeout('window.location="../country/"' + 'char',3000)
}
</script>
</head>

<button id="b2" onClick="myFuction('2')"> 2 </button>
</html>

Philip M
Sep 15th, 2007, 01:13 PM
<html>
<head>
</head>

<body>
<script type="text/javascript">
var char; // global variable
function myFunction(char) {
window.setTimeout("delayit()", 3000);
}
function delayit() {
window.location.href = "../country/" + char;
}
</script>

<button id="b2" onClick="myFunction('2')"> 2 </button>

</body>
</html>

BarrMan
Sep 15th, 2007, 01:47 PM
If you don't want to make an extra function for it you can just do this:

<html>
<head>
<script type="text/javascript">
function myFuction(char){
window.setTimeout("window.location.href = \'../country/" + char + "\';",3000)
}
</script>
</head>

<input type="button" id="b2" onClick="myFuction('2')" value="2" />
</html>

kamkam
Sep 15th, 2007, 01:47 PM
<html>
<head>
</head>

<body>
<script type="text/javascript">
var char; // global variable
function myFunction(char) {
window.setTimeout("delayit()", 3000);
}
function delayit() {
window.location.href = "../country/" + char;
}
</script>

<button id="b2" onClick="myFunction('2')"> 2 </button>

</body>
</html>

it's better now, it bring out a window, and get the right content from a specify file.

But the url from the address bar is not right, it give me the result like the following;
http://www.mysite.com/country/undefined/

it should be like the "http://www.mysite.com/country/2/"

but i does known why?

and i changed

function delayit() {
window.location.href = "../country/" + char;
}

to

function delayit() {
window.location.href = "/country/" + char + "/";
}


i have wrote a rewrite rule for the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ display.php?name1=$1&name2=$2 [L]

and i tested above with
<a href="/country/2/"> testing </a>

and it does work well, and the address bar give me the url like "http://www.mysite.com/country/2/"

kamkam
Sep 15th, 2007, 02:14 PM
If you don't want to make an extra function for it you can just do this:

<html>
<head>
<script type="text/javascript">
function myFuction(char){
window.setTimeout("window.location.href = \'../country/" + char + "\';",3000)
}
</script>
</head>

<input type="button" id="b2" onClick="myFuction('2')" value="2" />
</html>

it does not open a window for me, even i changed the myFunction to the following, it still does not open a window for me.

function myFuction(char){
window.setTimeout("window.location.href = \'/country/" + char + "/" + "\';",3000)
}

BarrMan
Sep 15th, 2007, 02:27 PM
In Firefox it works well but in IE I see the problem. It redirects it to the same page. I'll run some more tests.

kamkam
Sep 15th, 2007, 02:48 PM
thanks for everyone;
it works now, i rechanged Philip M code a litter bit, then it does the job now, it did not pass value properly in the last function.


<script type="text/javascript">
var char; // global variable
function myFunction(str) {
char=str;
window.setTimeout("delayit()", 3000);
}
function delayit() {
alert(char);
window.location.href = "/country/" + char + "/";
}

</script>

kamkam
Sep 15th, 2007, 02:51 PM
In Firefox it works well but in IE I see the problem. It redirects it to the same page. I'll run some more tests.

your one looks fine for me, but i could not find out why it does not open a window.

Philip M
Sep 15th, 2007, 07:43 PM
I forgot that char is a reserved word (keyword) in Javascript and may not be used as the name of a variable. Change it to something else.

kamkam
Sep 15th, 2007, 11:38 PM
I forgot that char is a reserved word (keyword) in Javascript and may not be used as the name of a variable. Change it to something else.


i see, that's the problem i have, thanks for your time, it does the job now.