javascript 如何在javascript中使用href?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20857847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:28:53  来源:igfitidea点击:

How to use href in javascript?

javascripthtmlgoogle-apps-script

提问by ashishSober

<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>

as i m using document.location && window.location both are not working to switch page,is there any other method which can help me out to

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

采纳答案by Mogsdad

You've left clues, including the google-apps-scripttag and the call to google.script.run, but your question should make it clearer that this code is being hosted from the Google Apps Script environment. That limits the solution, as described in the documentation for Class HtmlOutput.

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

You are trying to redirect the browser to a new URL, which is not allowed in Apps Script web apps.ref

您正在尝试将浏览器重定向到新的 URL,这在 Apps Script Web 应用程序中是不允许的。参考

That means that none of the other provided javascript solutions will work. Your work-around of presenting the user with a clickable link is your only option for redirecting to an "external" URL. (external == not your web app)

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

You can provide the illusion of a redirect to another html page within your Apps Script WebApp, though. See this answerfor an example of serving multiple html pages using HtmlService. The basic idea is to write doGet()to accept a query parameter that it will use to select which html page to serve.

不过,您可以在 Apps Script WebApp 中提供重定向到另一个 html 页面的错觉。有关使用 HtmlService 提供多个 html 页面的示例,请参阅此答案。基本思想是编写doGet()接受一个查询参数,它将用于选择要提供的 html 页面。

回答by Manish Jangir

you can use

您可以使用

 window.location.href

回答by Farhad

 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");
    }

回答by Orville Patterson

This is a simple thing. Before askin you should do research.

这是一件简单的事情。在问之前你应该做研究。

You can use:

您可以使用:

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");
    }

if you are not sure about anything please comment.

如果您不确定任何事情,请发表评论。

http://www.sivamdesign.com/scripts/navigate.htmlthere are a few examples which shows the scripts on how it works

http://www.sivamdesign.com/scripts/navigate.html有几个例子展示了它是如何工作的脚本

回答by ashishSober

    <?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!!");
}