2011-09-22 103 views
0

有人可以帮我解决这个问题。强迫在同一域的同一窗口中打开链接

当_blank位于外部域时,需要打开所有链接。但同一个域中的链接必须在同一个窗口中打开。我有问题,因为我工作在2个域名之一是https://www和其他只是没有www的http://,如何在没有www的链接上的同一窗口中打开链接?

function externalLinks() { 
    var h = window.location.host; 
    jQuery("a[href^='http']").not("[href*='" + h + "']").not(".forceSameWindow").attr('target', '_blank'); 
} 

现在所有的链接exept空白例如https://www.something.com开口道:http://something.com

我必须在jQuery的/ JS做到这一点。我通过做硬编码域来做到这一点,但是做得很好!

感谢您的帮助!

回答

0

你必须强制apache添加www。

添加到您的.htaccess文件:

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www.your_domain.com$ 
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301] 
+0

VisgičiaEN saitas,tai ir ENrašom。 –

0
var externalLinks = function(){ 
    var anchors = document.getElementsByTagName('a'); 
    var length = anchors.length; 
    for(var i=0; i<length;i++){ 
    var href = anchor[i].href; 
    if(href.indexOf('http://h4kr.com/') || href.indexOf('http://www.h4kr.com/')){ 
     return; 
    } 
    else{ 
     anchor[i].target='_blank'; 
    } 
    } 
}; 

这应该工作:)

2

只要改变

var h = window.location.host; 

var h = window.location.host.replace(/^www/,''); 

这样,如果你是www主机或没有也没关系

+0

工作,谢谢!!! – Renaldas

0

这是基于把你原来的职位

$("a").each(function($a){ 
    $a=$(this); 
    if(!~this.href.indexOf(document.location.host)||!$a.hasClass('forceSameWindow')){ 
     $a.attr('target','_blank'); 
    } 
}) 

假设各个环节都没有设置为_blank最初

相关问题