【问题标题】:Javascript: open new page in same windowJavascript:在同一窗口中打开新页面
【发布时间】:2010-09-21 00:41:38
【问题描述】:

是否有一种简单的方法可以修改此代码,以便在 SAME 窗口中打开目标 URL?

<a href="javascript:q=(document.location.href);void(open('http://example.com/submit.php?url='+escape(q),'','resizable,location,menubar,toolbar,scrollbars,status'));">click here</a>``

【问题讨论】:

    标签: javascript window


    【解决方案1】:
    <script type="text/javascript">
    window.open ('YourNewPage.htm','_self',false)
    </script>
    

    见参考: http://www.w3schools.com/jsref/met_win_open.asp

    【讨论】:

    • 这真的是我的救命稻草嘿嘿... :)
    • false 应该意味着您可以使用后退按钮返回到创建窗口的页面,对吧?这对我不起作用。
    【解决方案2】:

    window.open()的第二个参数是一个字符串,表示目标窗口的名称。

    将其设置为:“_self”。

    <a href="javascript:q=(document.location.href);void(open('http://example.com/submit.php?url='+escape(q),'_self','resizable,location,menubar,toolbar,scrollbars,status'));">click here</a>
    


    旁注: 以下问题概述了将事件处理程序绑定到 HTML 链接的更好方法。

    What's the best way to replace links with js functions?

    【讨论】:

      【解决方案3】:
      <a href="javascript:;" onclick="window.location = 'http://example.com/submit.php?url=' + escape(document.location.href);'">Go</a>;
      

      【讨论】:

        【解决方案4】:

        试试这个,它在 ie 7 和 ie 8 中对我有用

         $(this).click(function (j) {
                    var href = ($(this).attr('href'));
                    window.location = href;
                    return true;
        

        【讨论】:

          【解决方案5】:

          这对我有用:

          <button name="redirect" onClick="redirect()">button name</button>
          <script type="text/javascript">
          function redirect(){
          var url = "http://www.google.com";
          window.open(url, '_top');
          }
          </script>
          

          【讨论】:

            【解决方案6】:

            如果我是你,我会采取稍微不同的方式。在页面加载时更改文本链接,而不是在单击时更改。我将在 jQuery 中给出示例,但它可以很容易地在 vanilla javascript 中完成(虽然,jQuery 更好)

            $(function() {
                $('a[href$="url="]')    // all links whose href ends in "url="
                    .each(function(i, el) {
                        this.href += escape(document.location.href);
                    })
                ;
            });
            

            并像这样编写您的 HTML:

            <a href="http://example.com/submit.php?url=">...</a>
            

            这样做的好处是人们可以看到他们正在点击的内容(href 已经设置),并且它会从您的 HTML 中删除 javascript。

            说了这么多,看来你是在用PHP……为什么不把它加到服务器端呢?

            【讨论】:

              【解决方案7】:

              那么通过在href末尾添加URL,每个链接都会在同一个窗口中打开?您也可以在 HTML 中使用 _BLANK 来做同样的事情。

              【讨论】:

                【解决方案8】:

                试试

                <a href="#" 
                   onclick="location='http://example.com/submit.php?url='+escape(location)"
                   >click here</a>

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-02-03
                  • 2018-11-27
                  • 2017-03-29
                  • 1970-01-01
                  • 2012-03-18
                  • 1970-01-01
                  相关资源
                  最近更新 更多