Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.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重定向用户_Javascript_Jquery - Fatal编程技术网

无法使用javascript重定向用户

无法使用javascript重定向用户,javascript,jquery,Javascript,Jquery,我有一个页面,用户可以点击它向他的gmail好友发送邀请 这是按钮 <button onclick="TwkInvite()" class="btn"> <i class="fa fa-google"></i>Import your Gmail Contacts </button> 我已经尝试了window.location.href、window.location、location.href所有内容 这个功能被完美地执行,因为警报也被执

我有一个页面,用户可以点击它向他的gmail好友发送邀请

这是按钮

<button onclick="TwkInvite()" class="btn">
    <i class="fa fa-google"></i>Import your Gmail Contacts
</button>
我已经尝试了window.location.href、window.location、location.href所有内容

这个功能被完美地执行,因为警报也被执行,但我不知道为什么它不重定向到谷歌网站,每当我点击按钮,页面就会刷新


没有打印错误,我也检查了mozilla控制台,但没有发现错误

看起来像是权限问题

当我将其转换为代码片段时,在控制台中出现以下错误:

Refused to display 'https://accounts.google.com/o/oauth2/auth?client_id=*********-*************…/app/invite-send&scope=https://www.google.com/m8/feeds/&response_type=code' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
函数TwkInvite(){
window.location.href=”https://accounts.google.com/o/oauth2/auth?client_id=*********-*************.apps.googleusercontent.com和重定向uri=https://www.example.com/app/invite-send&scope=https://www.google.com/m8/feeds/&response_type=code";
}

导入你的Gmail联系人
代码中的所有内容都很好。 我尝试了以下操作,它正在运行:

<script type="text/javascript">
        function TwkInvite() {
        alert('function is called');
        window.location.href = "https://accounts.google.com/o/oauth2/auth?client_id=*********-*************.apps.googleusercontent.com&redirect_uri=https://www.example.com/app/invite-send&scope=https://www.google.com/m8/feeds/&response_type=code";
    }
    </script>

函数TwkInvite(){
警报(“调用函数”);
window.location.href=”https://accounts.google.com/o/oauth2/auth?client_id=*********-*************.apps.googleusercontent.com和重定向uri=https://www.example.com/app/invite-send&scope=https://www.google.com/m8/feeds/&response_type=code";
}

或者将代码包含在特定的javascript文件中,它将运行。

您的问题表明控制台中没有错误,这意味着导致重定向的是默认操作。它可能会提交一份我们所知的表格。这将阻止提交父表单(如果有)

您可以使用
return false
来防止这种情况发生

<button onclick="TwkInvite();return false;" class="btn">

看起来您并没有阻止按钮重新加载页面,至少在Chrome中是这样做的。要防止这种情况,必须防止触发默认事件

如下定义按钮,以便将事件参数转发给处理程序:

<button onclick="TwkInvite(event)" class="btn">

可能重复的
使用此选项。这将阻止家长表格被提交,如果有的话。你能看看这个我希望它会有所帮助。建议在location.href之后返回false或使用event.preventDefault()。@RejithRKrishnan将其写为答案,这样我就可以进行投票并将其标记为答案。如何在页面中包含javascript部分?
<button onclick="TwkInvite(event)" class="btn">
function TwkInvite(e) {
    alert('function is called');
    window.location.href = "https://accounts.google.com/o/oauth2/auth?client_id=*********-*************.apps.googleusercontent.com&redirect_uri=https://www.example.com/app/invite-send&scope=https://www.google.com/m8/feeds/&response_type=code";

    // The event is in window.event in IE
    var e =  e || window.event; 

    // e.prevenDefault() for modern browsers, e.returnValue for IE
    e.preventDefault ? e.preventDefault() : (e.returnValue = false);
}