jQuery 如何在使用 window.open 时停留在当前页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15299617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:46:30  来源:igfitidea点击:

How to Stay on Current page while using window.open

javascriptjquery

提问by Moiz

I am firing the Window.open();command. which opens the link page in another tab. what i want is when i click the link, the link will open in new window but should be on the same page.

我正在执行Window.open();命令。在另一个选项卡中打开链接页面。我想要的是当我点击链接时,链接将在新窗口中打开,但应该在同一页面上。

Is that possible ?

那可能吗 ?

presently i am using like this.

目前我正在使用这样的。

function AddToDatabase(url) {
            window.open(url, "_blank");
}

回答by Umair Saleem

Use _self instead of _blank.

使用 _self 而不是 _blank。

window.open(url, "_self");
  1. _blank - URL is loaded into a new window. This is default
  2. _parent - URL is loaded into the parent frame
  3. _self - URL replaces the current page
  4. _top - URL replaces any framesets that may be loaded name - The name of the window
  1. _blank - URL 加载到新窗口中。这是默认的
  2. _parent - URL 加载到父框架中
  3. _self - URL 替换当前页面
  4. _top - URL 替换可能加载的任何框架集 name - 窗口的名称

For further details. See This Link

欲知详情。看到这个链接

回答by Rusty Jeans

The code you have should not be changing the page. How are you calling AddToDatabase()? Is it from an a hreftag? If so the default action is taking place from the link and you need to prevent that.

您拥有的代码不应更改页面。你怎么打电话AddToDatabase()?它来自a href标签吗?如果是这样,默认操作是从链接发生的,您需要阻止它。

You can set the href attribute to javascript:void(0)

您可以将 href 属性设置为 javascript:void(0)

<a href="javascript:void(0)" onclick="AddToDatabase('myUrl')">Add URL</a>

Or you can have the onclick attribute return false

或者你可以拥有 onclick 属性 return false

<a href="#" onclick="AddToDatabase('myUrl'); return false;">Add URL</a>

回答by Programmer Dan

I know that this question is uber old, but it perfectly described the problem I needed answered, and I was able to come up with a decent solution, so I thought I'd post it here. It's an interesting workaround.

我知道这个问题已经很老了,但它完美地描述了我需要回答的问题,并且我能够想出一个不错的解决方案,所以我想我会在这里发布它。这是一个有趣的解决方法。

Essentially you set a timeout to delay forwarding the current page to the new url, and then open the current url in a new tab. So:

本质上,您设置超时以延迟将当前页面转发到新 url,然后在新选项卡中打开当前 url。所以:

function open_hidden_tab(new_url) {
    var thisTimeout= setTimeout(function() {
        window.location.href= new_url;
    }, 500);
    var newWindow= window.open(window.location.href);
    if(!newWindow) {
        clearTimeout(thisTimeout);
        alert('Please allow pop-ups on this site!');
    }
}
open_hidden_tab('https://google.com');

Obviously, you should show some kind of error message on your site instead of using the annoying alert function, but it works for the purposes of this example.

显然,您应该在您的站点上显示某种错误消息,而不是使用烦人的警报功能,但它适用于本示例的目的。

Hope this helps someone!

希望这可以帮助某人!

回答by Md Safiul Alam

you can use this.

你可以用这个。

 <a href="terms.html" onclick="window.open(this.href, 'targetWindow', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=600');return false;" />Terms & Conditions</a>

its works for me

它对我有用

回答by Josh Burgess

Here's an example of a pop-under:

下面是一个弹出窗口的示例:

var url = "example.com";
window.open(url, "s", "resizable=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, copyhistory=no").blur();
window.focus();

If that's what you're looking for, go nuts. It's extremely unethical from a web development sense, but it can be done.

如果这就是你要找的,那就去吧。从 Web 开发的角​​度来看,这是非常不道德的,但可以做到。

回答by SaidbakR

<html>
<body>

<script>
myWindow=window.open('','','width=200,height=100')
myWindow.document.write("<p>This is 'myWindow'</p>")
myWindow.focus()
</script>

</body>
</html>

You should determine the width and the height of the window.

您应该确定窗口的宽度和高度。

Look at this demo: http://jsfiddle.net/XyJW5/but don't forget to allow popup in your browser for this website

看看这个演示:http: //jsfiddle.net/XyJW5/但不要忘记允许在你的浏览器中弹出这个网站