2011-09-22 98 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最初

相關問題