Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
生成我的网站';Javascript中的URL_Javascript_Redirect - Fatal编程技术网

生成我的网站';Javascript中的URL

生成我的网站';Javascript中的URL,javascript,redirect,Javascript,Redirect,我有一个函数,负责为当前环境生成HTTP URL。当我在本地主机上工作时,该函数返回如下内容: http://localhost/mysite 在制作时,它应该只返回我网站的域名,如: http://mywebsite.org 情况并非总是如此。这个函数部分工作,我不明白为什么。我使用这个函数将AJAX表单提交到我的网站周围的PHP脚本,它可以工作,但是,当我尝试使用这个函数将用户重定向到一个特定的位置,该位置应该包含完整的HTTP URL,然后它会将我发送到localhost URL 以下

我有一个函数,负责为当前环境生成HTTP URL。当我在本地主机上工作时,该函数返回如下内容:

http://localhost/mysite
在制作时,它应该只返回我网站的域名,如:

http://mywebsite.org
情况并非总是如此。这个函数部分工作,我不明白为什么。我使用这个函数将AJAX表单提交到我的网站周围的PHP脚本,它可以工作,但是,当我尝试使用这个函数将用户重定向到一个特定的位置,该位置应该包含完整的HTTP URL,然后它会将我发送到localhost URL

以下是我编写的函数:

function generate_site_url()
{
    var domain = window.location.origin;
    if (domain.indexOf('localhost'))
    {
        return 'http://localhost/mysite/';
    }
    else
    {
        return 'http://mywebsite.org/';
    }
}
这是导致错误重定向问题的重定向功能:

function redirect(to, mode, delay, internal)
{
    mode     = (typeof mode === "undefined") ? 'instant' : 'header';
    delay    = (typeof delay === "undefined" || delay === null) ? 0 : delay;
    internal = (typeof internal === "undefined" || internal === true) ? true : false;

    if (to)
    {
        to = ((internal) ? generate_site_url() : '') + to;      
    }
    else
    {
        to = currentLocation(); 
    }

    setTimeout(function(){
        window.location = to;
    }, delay);
}
这是导致问题的
重定向的一个示例用法:

redirect('admin/index', 'header', 3000);
上面的函数调用正在将我发送到
http://localhost/mysite/admin/index
即使我正在生产,我的领域案例也应该适用

怎么了?我似乎想不出来了。

改变

if (domain.indexOf('localhost'))


为什么不直接返回Window.Location.HrefI只需要协议和主机名。
window.location.href
不也包括url路径名吗?在这种情况下,按window.location.pathname将其拆分并取第一个
if (domain.indexOf('localhost') != -1)
if (~domain.indexOf('localhost'))