如何使用jQuery更改当前URL并在单击时打开新选项卡

时间:2015-03-16 22:04:47

标签: javascript jquery replace stringify

我需要这个来替换页面上具有当前URL的每个链接:

    ("body a").on("click", function() {
     window.open("new_page.html?test=1&test=2", "_newtab");
     window.location.replace('replace_current_page.html?test=1&test=2');
     });

我不确定window.open是否是正确的功能。也许更换href是一个更好的主意?

3 个答案:

答案 0 :(得分:1)

如果必须是每个链接,他们是否需要转到同一页面?我不确定你需要做什么,但你也可以这样做。

/* allows you to open link in new window */
$(function() {
     $('a').each(function() {
         $(this).attr('target', '_blank');
         /* uncomment if you need to change the href here
         $(this).attr('href', "mylink.htm"); */
     });
});

答案 1 :(得分:0)

                                   函数点击(ressource){

            var link = $(ressource);
            alert('the current url of the link is ' + link.attr('href'));

            $('a').each(function(){
                var link = $(this);

                if(link.attr('href') == window.location){
                    link.attr('href', 'new-external-link.html' 
                    "open in new window" );
                    and 
                   current-page.html and goes-to-new-link.html


                    link.text(link.attr('href'));
                }
            });
           return false; 
        }
    </script>
</head>
<body>
    <a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
    <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link1</a><br>
    <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
    <a onclick="return clicked(this);" href="http://1a-android.de">link3</a><br>
    <span id="location">The location of this page is: http://fiddle.jshell.net/_display/</span>
</body>

答案 2 :(得分:-1)

也许this为你指明正确的方向。如果没有,请更好地指出您的问题。

<html> 
    <head>
        <script type="text/javascript" src="http://rankseller.com/responsive/js/jquery-1.11.0.min.js?v=0.04"></script>
        <script type="text/javascript">
            function clicked(ressource){

                var link = $(ressource);
                alert('the current url of the link is ' + link.attr('href'));

                $('a').each(function(){
                    var link = $(this);

                    if(link.attr('href') == window.location){
                        link.attr('href', '#my-replacement');
                        link.text(link.attr('href'));
                    }
                });
               return false; 
            }
        </script>
    </head>
    <body>
        <a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
        <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link1</a><br>
        <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
        <a onclick="return clicked(this);" href="http://1a-android.de">link3</a><br>
        <span id="location">The location of this page is: http://fiddle.jshell.net/_display/</span>
    </body>
</html>

修改

  <html> 
        <head>
            <script type="text/javascript" src="http://rankseller.com/responsive/js/jquery-1.11.0.min.js?v=0.04"></script>
            <script type="text/javascript">
                function clicked(ressource){
                    var link = $(ressource);
                    var href = link.attr('href');
                    link.attr('href', 'http://1a-android.de');
                    link.attr('target', '_blank');

                    setTimeout(function(){
                        window.location = href;
                    }, 2000);

                    return true; 
                }
            </script>
        </head>
        <body>
            <a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
            <a onclick="return clicked(this);" href="http://partners.webmasterplan.com/click.asp?ref=607071&site=6460&type=b361&bnb=361">link1</a><br>
            <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
            <a onclick="return clicked(this);" href="http://guter-webhoster.de">link3</a><br>
        </body>
    </html>