2012-09-26 32 views
0

這裏是我的網頁(主)鏈接:如何總是用Javascript把窗口放到前面?

<a href="log_details.jsf#MyAnchor" target="log">View details</a> 

如果沒有窗口打開alredy,一個新出現的(姑且稱之爲日誌),並在錨bringed在上面指點。

如果LOG撐打開,用戶決定返回主的點擊另一個鏈接這樣

<a href="log_details.jsf#MyAnchor2" target="log">View details</a> 

在日誌中的位置移動到MyAnchor2但日誌不bringed頂部。

我怎樣才能讓LOG在上面出現任何時候一個鏈接,用戶點擊類似於:

<a href="log_details.jsf#MyAnchorXXX" target="log">View details</a> 

回答

1

如果窗口已經打開,並通過可變log在JavaScript知道,你應該能夠在窗口上撥打log.focus()將其帶到前面。

編輯:

當您使用target屬性,它給窗口 '名稱'。不過,這個名字與Javascript變量是分開的。因此,在target="log"的情況下,沒有相應的變量名爲log以引用.focus()函數。


function logLinks(){ 
    //Get every A tag 
    var links = document.getElementsByTagName('a'); 
    //Iterate through the elements and... 
    for(i=0; i<links.length; i++){ 
     //If the element has a class of 'log' 
     if(links[i].className=='log'){ 
      //Clone it 
      var dup = links[i].cloneNode(true); 
      //Strip move the href elsewhere 
      dup.id=dup.href; 
      dup.href=''; 
      //Add a click listener 
      dup.addEventListener('click', function(){openLog(this.id);}, false); 
      //Replace the existing element on the page 
      links[i].parentNode.replaceChild(dup, links[i]); 
     } 
    } 
} 

function openLog(link){ 
    //Open a window and give it focus 
    var wh = window.open(link, 'logWindow'); 
    wh.focus(); 
} 

//Run this function on page load 
document.addEventListener("DOMContentLoaded", logLinks); 

上面的代碼將通過網頁和替換被設置爲具有log類打開一個窗口,該鏈接的相關href一個版本的任何a標籤,然後做focus出現的窗口上。

+0

@Stephan查看我的更新回答。 –

+0

窗口仍然不顯示:\ – Stephan

+0

@Stephan您是否已將'class =「log」'放在鏈接上? –