Javascript重定向与SetTimout无法正常工作

编程入门 行业动态 更新时间:2024-10-25 08:27:31
本文介绍了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无法正常工作

本文发布于:2023-10-12 17:26:47,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   重定向   工作   Javascript   SetTimout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!