Javascript重定向与SetTimout无法正常工作 [英] Javascript Redirect with SetTimout Not Working

查看:110
本文介绍了Javascript重定向与SetTimout无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎无法获得此JavaScript重定向工作?
建议?



我应该使用元刷新还是怎么做?

  //如果我们在GeoSuccess(event)
{
document.getElementById(Latitude)。value = event.coords.latitude ;
document.getElementById(Longitude)。value = event.coords.longitude;
document.getElementById(location)。href =track.cfm?track = s& Lat =+ event.coords.latitude +& Long =+ event.coords.longitude;

var redirectUrl =track.cfm?track = s& Lat =+ event.coords.latitude +& Long =+ event.coords.longitude;



//如果Geoocation请求
函数出现错误GeoError(event)
{
alert(Error代码+ event.code +。+ event.message);
}

函数重定向()
{
window.location = redirectUrl;
}
setTimeout(重定向,15000);


解决方案

问题在于范围 redirectUrl 变量。您将 redirectUrl 声明为本地 onGeoSuccess 函数,所以它仅在其内部可见。对于解决方法,您可以放置​​所有这些东西:

 函数redirect()
{
window.location = redirectUrl;
}
setTimeout(重定向,15000);

onGeoSuccess 函数中,或者使 redirectUrl 全局,通过在 redirectUrl 之前删除 var

  redirectUrl =track.cfm?track = s& Lat =+ ... 
// ^ ---- -no'var'


Can't seem to get this javascript redirect to work? Suggestions?

Should I maybe do it with a meta refresh and how?

// If we have a successful location update
function onGeoSuccess(event)
{
    document.getElementById("Latitude").value =  event.coords.latitude; 
    document.getElementById("Longitude").value = event.coords.longitude;
    document.getElementById("location").href = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude;

var redirectUrl = "track.cfm?track=s&Lat=" + event.coords.latitude + "&Long=" + event.coords.longitude; 

}

// If something has gone wrong with the geolocation request
function onGeoError(event)
{
    alert("Error code " + event.code + ". " + event.message);
}

function redirect() 
     { 
    window.location = redirectUrl; 
     } 
    setTimeout(redirect,15000); 

解决方案

The problem is the scope of redirectUrl variable. You declared redirectUrl as local for onGeoSuccess function, so it will be visible only inside of it. For workaround ,you can put all this stuff:

function redirect() 
{ 
    window.location = redirectUrl; 
} 
setTimeout(redirect,15000);

inside of onGeoSuccess function, or make redirectUrl global, by removing var before redirectUrl:

     redirectUrl = "track.cfm?track=s&Lat="+...
//^-----no 'var'

这篇关于Javascript重定向与SetTimout无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆