简体   繁体   English

新 Window 在将 target="_blank" 与 onclick 一起使用时无法打开

[英]New Window not Opening when using target="_blank" with onclick

Summary: 1. Need to open a new window to display a survey from a third party.总结: 1.需要新开一个window来展示一个第三方的调查。
2. Cannot use JavaScript to open the window (currently using target="_blank". 3. Must use JavaScript to track clicks (currently using onclick). 4. original window must NOT change its content upon clicking. 2. 不能使用 JavaScript 打开 window(当前使用 target="_blank")。 3. 必须使用 JavaScript 来跟踪点击(当前使用 onclick)。 4. 原始 window 不能在点击时更改其内容。

I have a JSP that is trying to launch a third party survey in a new window. Originally, the code was launching the new window like this:我有一个 JSP 试图在新的 window 中启动第三方调查。最初,代码是这样启动新的 window 的:

<a id="customer_survey_button"  href="#" target="_blank" onclick="recordOutboundLink(this, 'Outbound Links', 'blah.com');openChildWindow('http://www.blah.com/f.asp?etcetera','TAKE SURVEY');return false;"  class="genbut">TAKE SURVEY</a>

It worked in FF, but not Chrome.它适用于 FF,但不适用于 Chrome。 It caused a security problem with Chrome.它导致了 Chrome 的安全问题。 Something like an 'unsafe JavaScript attempt to access a frame (new window) which loads one domain, while it's origin is another domain.类似于“不安全的 JavaScript 尝试访问加载一个域的框架(新窗口),而它的来源是另一个域。 My solution was to open the window using HTML, like this:我的解决方案是使用 HTML 打开 window,如下所示:

<a id="customer_survey_button"  href="http://www.blah.com/f.asp?etcetera" target="_blank" onclick="recordOutboundLink(this, 'Outbound Links', 'blah.com');return false;"  class="genbut">Take Survey</a>

The problem is, that is not opening in a new window. During debugging, I removed the onclick from the above code, and it does open in a new window. I'm not sure if it is something wrong in the JS code...问题是,它没有在新的 window 中打开。在调试过程中,我从上面的代码中删除了 onclick,它确实在新的 window 中打开。我不确定它是否是 JS 代码中的错误。 .

<script type="text/javascript">
<!--
var pageTracker = _gat._getTracker("<%=googleTrackerId%>");
pageTracker._initData();
pageTracker._trackPageview();

function recordOutboundLink(link, category, action) {
  try {    
    pageTracker._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}
// -->
</script>

...or if it is just a matter of using target and onclick in the same anchor tag. ...或者是否只是在同一个锚标记中使用目标和 onclick 的问题。 Any thoughts?有什么想法吗?

I researched elsewhere, and on Stack Overflow, I found the below results, but none seemed to address this situation.我在其他地方进行了研究,在 Stack Overflow 上,我发现了以下结果,但似乎没有一个能解决这种情况。 For example(s):举些例子):

This helped in one way, because I was able to test the removal of the "return false".这在某种程度上有所帮助,因为我能够测试“return false”的删除。 That had the same result as it did for "user1223427"- it rendered the same page in two different windows. It did not help because I need a non-JavaScript way to open the target URL (due to the security issues mentioned).这与“user1223427”的结果相同——它在两个不同的 windows 中呈现相同的页面。它没有帮助,因为我需要一种非 JavaScript 的方式来打开目标 URL(由于提到的安全问题)。 OnClick for google analytics and target _blank. OnClick 用于谷歌分析和目标 _blank。 The link does not work? 链接失效了?

This one doesn't help, because I'm not using JQuery and I don't understand the connection between the JQuery solution and my situation select onclick that contains _blank这个没有帮助,因为我没有使用 JQuery 并且我不了解 JQuery 解决方案与我的情况select onclick 之间的联系,其中包含 _blank

This only applies to getting tracking code to work, not issues with a new window opening: Google Analytics - Download clicks tracking这仅适用于使跟踪代码正常工作,不适用于新的 window 开放问题: Google Analytics - 下载点击跟踪

I thought this one was going to have a good result, but it's solution is a JS one.我认为这个会有很好的结果,但它的解决方案是 JS 的。 Bug in external link tracking when opening the link in a new window in Google Analytics? 在 Google Analytics 中打开新的 window 中的链接时,外部链接跟踪中存在错误?

The recordOutboundLink function is opening the link. recordOutboundLink function 正在打开链接。

//This is changing the URL to the href of the link you clicked    
setTimeout('document.location = "' + link.href + '"', 100) 

Your second example is returning false from the onclick event.您的第二个示例是从 onclick 事件返回 false。 This will prevent the anchor tag from performing it's normal behavior.这将阻止锚标记执行其正常行为。

If you comment out the setTimeout line, and remove return false from the onclick, you should see the link behave as you want.如果您注释掉 setTimeout 行,并从 onclick 中删除 return false,您应该会看到链接的行为如您所愿。

You could also modify the setTimeout function to call window.open instead of document.location您还可以修改 setTimeout function 以调用 window.open 而不是 document.location

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在HTML中为target =“_ blank”打开新窗口 - Opening new window in HTML for target=“_blank” 使用JavaScript在新窗口/选项卡(target = _blank)中检测功能 - Feature detect opening in a new window/tab(target=_blank) with JavaScript 使用 target="_blank"/window.open() 打开新窗口/选项卡时如何防止 sessionStorage 被继承? - How to prevent sessionStorage being inherited when using target="_blank"/window.open() to open a new window/tab? target =“ _ blank”在Edge中打开新的空白页 - target= “_blank” opening new blank pages in Edge 单击带有target = _blank的LinkBut​​ton后,如何防止回发在新窗口中打开? - How do I prevent postbacks from opening in new window after a LinkButton with target=_blank is clicked? 在PHP(Wordpress)中,如何更改此代码以使Google+链接target = blank在新窗口中打开? - In PHP (Wordpress), how can I alter this code to make the Google+ link target=blank, opening in a new window? jQuery添加目标_blank不在新标签页中打开 - jQuery adding target _blank not opening in new tab electron.js 中 webview 中基于 target=_blank 的链接在父级 window 中 nativeWindowOpen 为真时未打开 - target=_blank based link in webview in electron.js are not opening when nativeWindowOpen is true in parent window Window.open打开空白的新标签? - Window.open opening blank new tab? 在模式而不是新窗口中打开目标空白 - Open target blank in modal instead of new window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM