target=_blank doesn't work in IE8, running IEs

2020-02-15 05:22发布

IE8 will sometimes prevent links from spawning if they have target=_blank set.

This problem appears to be limited to corrupt installs of IE, such as when installing several versions side-by-side.

I edited this question once I found the answer, and hopefully this will save someone else some time. The answer is in the comments of the first answer listed.

5条回答
Root(大扎)
2楼-- · 2020-02-15 05:57

I know this is already answered, but I just wanted to tell about the jQuery's live binding functionality:

$("a.myclass").live("click",function() {
    $(this).attr("target","_blank");
});

This example sets the 'target="_blank"' attribute to any link with the class "myclass", even those created with Javascript.

查看更多
▲ chillily
3楼-- · 2020-02-15 05:57

This is because target="_blank" is not valid under XHTML Strict Mode. See:

http://www.8164.org/xhtml-strict/

The following should work in all cases.

<script>window.open("http://www.80vul.com/test/ie8-1.htm");</script>

I presume that as of IE8 Beta 1, the default mode is now STRICT instead of TRANSITIONAL.

查看更多
来,给爷笑一个
4楼-- · 2020-02-15 06:13

This depends upon which stand-alone IE8 that you use. I found this to be a problem while using "Final Builds Site - Internet Explorer Collection" (http://finalbuilds.edskes.net/iecollection.htm) version 1.6.0.3. The developer has now fixed this bug as of Ver. 1.6.0.4, and links with target="_blank" now work as expected.

查看更多
贪生不怕死
5楼-- · 2020-02-15 06:13

Yeah, XHTML Strict Mode doesn't accept target="_blank".

If you don't want to keep using window.open everywhere, you can use rel="external" and some extra Javascript like the following, using JQuery:

$(document).ready(function() {
    $("a[rel='external']").attr("target","_blank");
});

EDIT: To set all generated links:

 $("a[rel='external']").ready(function() { 
    $("a[rel='external']").attr("target","_blank");
 });

Or, without jQuery, you can use the script, found here:

function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 
window.onload = externalLinks;
查看更多
Root(大扎)
6楼-- · 2020-02-15 06:15

What about if you use target='blank' ? I know it's not THE same, but you will get the popup/window open in a new instance, and your site could validate for XHTML Strict Mode :)

查看更多
登录 后发表回答