单击链接时打开带有URL的选项卡

时间:2013-12-24 09:45:56

标签: c# javascript jquery html asp.net-mvc

我想在点击“条款和条件”链接时在同一浏览器中打开一个新标签,该链接应返回网址:http://localH:30321/OrchardLocal/TermsAndConditions

我到目前为止:

<p>Accept the <a href="~/" target="_blank" onclick="newwin()"> <span class="big-red">Terms and Conditions</span></a></p>
<script>
    function newwin() {
        window.open("~/TermsAndConditions");
    }
</script>

这实际上会在同一浏览器中打开2个新标签页。

http://localH:30321/OrchardLocal/Users/Account/~/TermsAndConditions
由于显而易见的原因,无法找到抛出页面的

广告然后第二个标签显示主页:

http://localH:30321/OrchardLocal/

尝试删除href,其中一个〜/ ......似乎无法得到它......任何帮助人员? 感谢

5 个答案:

答案 0 :(得分:3)

首先抱歉错误的答案: 因为你想在同一个浏览器的新标签页面中打开网址,这是不可能的。这完全取决于浏览器的设置。

http://forums.asp.net/t/1902352.aspx

Open a URL in a new tab (and not a new window) using JavaScript

如果浏览器设置是默认设置(为FF测试)

这将有效Open new tab in same browser window

function openinnewTab() {
    var win = window.open("~/TermsAndConditions", '_blank');
    win.focus();
}

答案 1 :(得分:2)

试试这个

function newwin(e) {
    e.preventDefault();
    window.open("/TermsAndConditions", '_blank');
}

顺便说一下你不能直接在javascript中使用“〜”..如果你想使用这样的服务器代码

function newwin(e) {
e.preventDefault();
window.open('<%= ResolveUrl("~/TermsAndConditions") %>', '_blank');
}

修改

<p>Accept the <a href="javascript: void(0)" target="_blank" onclick="newwin()"> <span class="big-red">Terms and Conditions</span></a></p>

JS

function newwin(e) {

    window.open('<%= ResolveUrl("~/TermsAndConditions") %>', '_blank');

    }

     function newwin(e) {

            window.open("/TermsAndConditions", '_blank');

        }

OR pure asp.net code

<p>Accept the <a runat="server" href="~/TermsAndConditions" target="_blank" > <span class="big-red">Terms and Conditions</span></a></p>

你必须添加runat =“server”属性

答案 2 :(得分:1)

<a href='JavaScript:newwin('<%=ResolveClientUrl("~/OrchardLocal/TermsAndConditions")%>');'  >

<script>
    function newwin(url) {
        window.open(url,'_blank');
    }
</script>

答案 3 :(得分:0)

一个标签用于您的href,另一个用于您的onclick。 sajad-lfc给出了严谨的答案。

答案 4 :(得分:0)

尝试以下代码

<p>Accept the <a href="~/" target="_blank" onclick="newwin()"> <span class="big-red">Terms and Conditions</span></a></p>
<script>
    $("span.big-red").on("click", function () {
        window.open('www.yourdomain.com', '_blank');
    });
</script>

这完美有效,希望对你有用。