Javascript 如何在location.href中使用目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8944697/
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-24 08:10:13  来源:igfitidea点击:

How to use target in location.href

javascriptwindow.open

提问by user1084509

I am currently developing a web application where I need to open a popup window to show a report. The problem is that some versions of explorer don't support the window.open javascript function, so when this is the case I catch the error and open the new url with location.href. Here the code:

我目前正在开发一个 Web 应用程序,我需要在其中打开一个弹出窗口来显示报告。问题是某些版本的资源管理器不支持 window.open javascript 函数,因此在这种情况下,我会捕获错误并使用 location.href 打开新 url。这里的代码:

try {
    window.open(url, "","width=1002,height=700,location=0,menubar=0,scrollbars=1,status=1,resizable=0")
} catch(e) {
    location.target = "_blank";
    location.href = url;
}

The problem is that the location.target is not working and I would like to know if there is a way to specify the target of the location.href so it can be opened in a new tab.

问题是 location.target 不起作用,我想知道是否有办法指定 location.href 的目标,以便可以在新选项卡中打开它。

采纳答案by T.J. Crowder

The problem is that some versions of explorer don't support the window.open javascript function

问题是某些版本的资源管理器不支持 window.open javascript 功能

Say what? Can you provide a reference for that statement? With respect, I think you must be mistaken. This workson IE6 and IE9, for instance.

说什么?您能否提供该声明的参考?恕我直言,我想你一定是误会了。例如,这适用于 IE6 和 IE9。

Most modern browsers won't let your code use window.openexcept in direct response to a user event, in order to keep spam pop-ups and such at bay; perhaps that's what you're thinking of. As long as you only use window.openwhen responding to a user event, you should be fine using window.open — with all versions of IE.

大多数现代浏览器不会让您的代码使用window.open除非直接响应用户事件,以防止垃圾邮件弹出窗口等;也许这就是你的想法。只要你只window.open在响应用户事件时使用,你应该可以使用window.open - 所有版本的 IE。

There is no way to use locationto open a new window. Just window.openor, of course, the user clicking a link with target="_blank".

没有办法用来location打开一个新窗口。只是window.open或者,当然,用户单击带有 的链接target="_blank"

回答by qiao

try this one, which simulates a click on an anchor.

试试这个,它模拟点击一个锚点。

var a = document.createElement('a');
a.href='http://www.google.com';
a.target = '_blank';
document.body.appendChild(a);
a.click();

回答by jojo

You can use this on any element where onclick works:

您可以在 onclick 工作的任何元素上使用它:

onclick="window.open('some.htm','_blank');"

onclick="window.open('some.htm','_blank');"

回答by bobbiloo

You could try this:

你可以试试这个:

        <script type="text/javascript">
         function newWindow(url){
                window.open(url);
            }
        </script>

And call the function

并调用函数

回答by Milos

If you go with the solution by @qiao, perhaps you would want to remove the appended child since the tab remains open and subsequent clicks would add more elements to the DOM.

如果您采用@qiao 的解决方案,也许您想删除附加的子项,因为该选项卡保持打开状态,后续单击会向 DOM 添加更多元素。

// Code by @qiao
var a = document.createElement('a')
a.href = 'http://www.google.com'
a.target = '_blank'
document.body.appendChild(a)
a.click()
// Added code
document.body.removeChild(a)

Maybe someone could post a comment to his post, because I cannot.

也许有人可以对他的帖子发表评论,因为我不能。

回答by cheeken

If you are using an <a/>to trigger the report, you can try this approach. Instead of attempting to spawn a new window when window.open()fails, make the default scenario to open a new window via target(and prevent it if window.open()succeeds).

如果您正在使用 an<a/>来触发报告,您可以尝试这种方法。不要在window.open()失败时尝试生成新窗口,而是将默认场景设置为通过打开新窗口target(并在window.open()成功时阻止它)。

HTML

HTML

<a href="http://my/url" target="_blank" id="myLink">Link</a>

JS

JS

var spawn = function (e) {
    try {
        window.open(this.href, "","width=1002,height=700,location=0,menubar=0,scrollbars=1,status=1,resizable=0")
        e.preventDefault(); // Or: return false;
    } catch(e) {
    // Allow the default event handler to take place
    }
}

document.getElementById("myLink").onclick = spawn;

回答by box86rowh

Why not have a hidden anchor tag on the page with the target set as you need, then simulate clicking it when you need the pop out?

为什么不在页面上设置一个隐藏的锚标签,并根据需要设置目标,然后在需要弹出时模拟单击它?

How can I simulate a click to an anchor tag?

如何模拟对锚标记的点击?

This would work in the cases where the window.open did not work

这将在 window.open 不起作用的情况下起作用

回答by Ma?l Nison

As of 2014, you can trigger the click on a <a/>tag. However, for security reasons, you have to do it in a clickevent handler, or the browser will tag it as a popup (some other events may allow you to safely trigger the opening).

从 2014 年开始,您可以触发对<a/>标签的点击。但是,出于安全原因,您必须在click事件处理程序中执行此操作,否则浏览器会将其标记为弹出窗口(某些其他事件可能允许您安全地触发打开)。

jsFiddle

js小提琴

回答by Paulo

<a href="url" target="_blank"> <input type="button" value="fake button" /> </a>