如何在javascript中使用href?

时间:2013-12-31 13:04:42

标签: javascript html google-apps-script

<label>
    <input type="button" onclick="abc()" id="log" value='SUBMIT'>
</label>
<script>
    function abc() {
        google.script.run.withSuccessHandler(callback).processForm(document.forms[0]);
    }

    function callback(ste) {
        if (ste == "true") {
            var a = document.getElementById('log');
            document.write(a.value);
            document.write('<a href="http://www.w3schools.com/js/js_htmldom_html.asp">do stuff</a>');
        } else document.write("false");
    }
</script>

因为我正在使用document.location&amp;&amp; window.location都不能切换页面,有没有其他方法可以帮助我

5 个答案:

答案 0 :(得分:2)

你可以使用

 window.location.href

答案 1 :(得分:0)

 function callback(ste) {
        if (ste == "true") {
            var a = document.getElementById('log');
            location.href='http://yoursite.com';
//or put your url into var and put it there
        } else document.write("false");
    }

答案 2 :(得分:0)

这很简单。在询问之前你应该做研究。

您可以使用:

function callback(ste) {
        if (ste == "true") {
            var a = document.getElementById('log');
            window.location.href='http://yoursite.com';
//or put your url into var and put it there
        } else document.write("false");
    }

如果您不确定,请发表评论。

http://www.sivamdesign.com/scripts/navigate.html有一些示例显示了脚本的工作原理

答案 3 :(得分:0)

您已经离开了线索,包括标记和对google.script.run的调用,但您的问题应该更清楚地表明此代码是从Google Apps脚本环境托管的。这限制了解决方案,如Class HtmlOutput的文档中所述。

您正尝试将浏览器重定向到新的网址,这在Apps脚本网络应用中是不允许的。 ref

这意味着其他任何提供的javascript解决方案都不会起作用。为用户提供可点击链接的解决方法是重定向到“外部”URL的唯一选择。 (外部==不是您的网络应用程序)

但是,您可以在Apps脚本WebApp中提供重定向到另一个html页面的错觉。有关使用HtmlService提供多个html页面的示例,请参阅this answer。基本思路是编写doGet()来接受一个查询参数,该参数将用于选择要提供的html页面。

答案 4 :(得分:0)

    <?var url=getScriptUrl();?>
    <a href='<?=url?>?page=form'>
           <label><input type="button" onclick="abc()" id="log" value='SUBMIT' class= "btn btn-danger"></label>
    </a>
    </form>

<script>
function abc(){
google.script.run.withSuccessHandler(callback).processForm(document.forms[0]);
}

function callback(ste){
if(ste=="true"){
alert("i LOGIN");
// want to switch it here
}
else
alert("Login Failed!!");
}