【问题标题】:How to use href in javascript?如何在javascript中使用href?
【发布时间】:2014-01-18 10:03:48
【问题描述】:
<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 && window.location 都无法切换页面,有没有其他方法可以帮助我

【问题讨论】:

  • 您在哪里使用 document.location?
  • 不确定,我确实理解了您的问题。
  • 我用 document.write 代替,但没有用

标签: javascript html google-apps-script


【解决方案1】:

你可以使用

 window.location.href

【讨论】:

    【解决方案2】:
     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");
        }
    

    【讨论】:

      【解决方案3】:

      这是一件简单的事情。在询问之前,您应该进行研究。

      你可以使用:

      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 有几个例子展示了脚本的工作原理

      【讨论】:

      • 它不起作用 ..window.location.href 和 location.href,两者都不起作用..如果我像 document.write('yoursite.com';">link</a>');如果我喜欢它会转到下一页并显示链接,然后必须点击该链接
      • 你想做什么?
      • 表示..我只是想直接切换到另一个页面,
      • 但是你的 window.location.href 和 location.href 不起作用,
      • 您可能想使用 firebug 来调试您的 javaScript,因为它应该可以工作,我能想到的唯一问题是您的函数有错误
      【解决方案4】:

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

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

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

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

      【讨论】:

      • 是的,环境确实在 Google Apps 脚本中,非常感谢
      • 但我知道我们如何在任何条件运算符中使用 : .. .
      • 好的..正如您在链接中提到的那样 确实是直接作用于按钮。 ..但我需要在“if”语句中使用... function abc(){ google.script.run.withSuccessHandler(callback).processForm(document.forms[0]); } 函数回调(ste){ if(ste=="true"){ alert("i LOGIN"); //想在这里切换我的页面 } else alert("Login Failed!!"); }
      • 是的,我也这样做了...你是的,这是不可能的...实际上我试图创建一个登录页面...不使用会话...所以小事情变得讨厌了..谢谢顺便说一句,你这么多..
      【解决方案5】:
          <?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!!");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 2021-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-23
        相关资源
        最近更新 更多