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

javascript - How to open a new window in the same page - Stack Overflow

programmeradmin0浏览0评论

I have a button:

<button>Click</button>

I want click it and open a new window in the same page, I bind the click event an event handle:

$('button').click(function () {
    var newurl="http://" + window.location["host"] + ";
    window.open(newurl, '_parent');
})

But is always open in new tab or new window, the second parameter I have try _self _top. I even have try window.location.href(newurl)

So how can I solve this problem?Does it matter with browser or OS? I view it in Mac OS's firefox and chrome.

I have a button:

<button>Click</button>

I want click it and open a new window in the same page, I bind the click event an event handle:

$('button').click(function () {
    var newurl="http://" + window.location["host"] + ";
    window.open(newurl, '_parent');
})

But is always open in new tab or new window, the second parameter I have try _self _top. I even have try window.location.href(newurl)

So how can I solve this problem?Does it matter with browser or OS? I view it in Mac OS's firefox and chrome.

Share Improve this question edited Feb 20, 2014 at 23:04 tshepang 12.5k25 gold badges97 silver badges139 bronze badges asked Jun 28, 2012 at 10:54 hh54188hh54188 15.7k35 gold badges116 silver badges191 bronze badges 3
  • What you ask is impossible. "New window" is mutually exclusive with "same page." It sounds like you really just want to change the URL in the current window/tab. – Matt Ball Commented Jun 28, 2012 at 10:55
  • Do you wish to open the url in the current window? I am not sure whether or not I understand your question right. – EricG Commented Jun 28, 2012 at 10:56
  • 1 @EricG: Yes, you understand is right – hh54188 Commented Jun 28, 2012 at 10:58
Add a ment  | 

4 Answers 4

Reset to default 4

Well that's not possible technically, it also depends on browser tab settings, window settings and third party tab manipulation plugins or addons.


Update

OP has cleared the confusion of all through his ment below his question, this is what you need to set a new url for current window:

$('button').click(function () {
    var newurl="http://" + window.location["host"];
    window.location.href = newurl; // or simply window.location
})

window.location =newurl; will open a new window replacing the parent

Have you tried this?

<script type="text/javascript">
  window.open ('test.htm','_self',false)
</script>

If you mean you want to replace the current page, window.location = newLocation; will do. If you want a modal popup, try JQuery UI Dialog.

发布评论

评论列表(0)

  1. 暂无评论