位置对象Javascript

时间:2012-07-17 12:37:03

标签: javascript

我在这个文件夹结构中有一个php文件:

网络 - >脚本 - > gen.php

从这个gen.php我想用Javascript中的location()函数重定向到

基于Web>的index.html

我试着这样做:

location = "localhost/Web/index.html"

但我只得到404 error说这条路径无效。这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:4)

在JavaScript中,

location不是一个函数。这是一个可以为其赋值的属性。

任何(假设Web在URL上公开,而不仅仅是内部目录):

  • http://localhost/Web/index.html
  • //localhost/Web/index.html
  • /Web/index.html
  • index.html

但是,重定向通常应该使用HTTP处理,因此您最好使用:

<?php
    header('Location: http://localhost/Web/index.html');
    exit;
?>

(请注意,虽然大多数浏览器都会从位置标头中的相对URI恢复,但规范需要绝对URI。)

答案 1 :(得分:2)

改为写location = "http://localhost/Web/index.html"。您错过了添加http://

如果您在同一目录中,则location = "index.html"