最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.open(url) not opening new web page in the same tab - Stack Overflow

programmeradmin4浏览0评论

window.open("index.php"); does not open the new page in the same tab, it opens it in a new tab.

I tried window.open("index.php",'_self') as well which does not open the tab at all.

Here is my code :

$.ajax({
    url: "login.php",
    type: 'POST',
    data : "username="+name+"&password="+pwd ,
    datatype :"text",
    async: false, 
    cache: true,
    timeout: 30000,
    error: function() {
        return true;
    },
    success: function(msg) {                        

       if(msg == "Validated")
        {
            alert(msg);
            window.open("index.php");
        }
        if(msg=="Incorrect password")
        {
            alert(msg);
            location.reload();                         
        }    
    }
});

window.open("index.php"); does not open the new page in the same tab, it opens it in a new tab.

I tried window.open("index.php",'_self') as well which does not open the tab at all.

Here is my code :

$.ajax({
    url: "login.php",
    type: 'POST',
    data : "username="+name+"&password="+pwd ,
    datatype :"text",
    async: false, 
    cache: true,
    timeout: 30000,
    error: function() {
        return true;
    },
    success: function(msg) {                        

       if(msg == "Validated")
        {
            alert(msg);
            window.open("index.php");
        }
        if(msg=="Incorrect password")
        {
            alert(msg);
            location.reload();                         
        }    
    }
});
Share Improve this question edited Apr 20, 2016 at 9:21 Jeroen 63.9k47 gold badges228 silver badges366 bronze badges asked Apr 15, 2013 at 12:44 Nitin Kundapur BhatNitin Kundapur Bhat 1851 gold badge2 silver badges7 bronze badges 3
  • 2 That's the whole point of window.open() - to open a new window. If you want to update the current tab, just set window.location.href to your URL. – Pointy Commented Apr 15, 2013 at 12:45
  • i have tried this ..... window.location.href for somereason does not work for me .....stackoverflow./questions/16002938/… – Nitin Kundapur Bhat Commented Apr 15, 2013 at 12:47
  • Setting window.location.href to a new value works for everybody. – Pointy Commented Apr 15, 2013 at 12:49
Add a ment  | 

5 Answers 5

Reset to default 5

Instead of window.open you should use window.location = "http://...."

The window.open function opens a new window(or tab). The window.location changes the url the current tab.

window.location is the function/property you should look at.

window.open will open in new tab if action is synchronous and invoked by user. If you remove async: false from ajax options (and this method is invoked by user for example by clicking a button), then new window will open instead of new tab. For simple navigation set window.location.href

As far as I know, window.location doesn't do this. The right method to do this is:

document.location = 'url-you-want-to-open.ext';

Best thing is to either include the full path (if it's on a different domain) or the absolute path if it's on the same domain. Only use relative path if the destination document is in the same folder.

To add to this:

window = speaks to the browser and its tabs

document = speaks to the current document that's loaded in the browser / tab.

发布评论

评论列表(0)

  1. 暂无评论