2016-01-22 19 views
-1

我在我的sharepoint搜索框中使用JavaScript。但window.location.replace功能不起作用?這裏是我的代碼:window.location.replace在我的sharepoint站點中不工作

$('.SearchBtnGo').click(function(){ 
    var url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl; 
    var value=$('#ctl00_ctl55_csr_sbox').val(); //Taking value from search box 
    var SearchUrl = url+"/_layouts/15/osssearchresults.aspx?k="; 
    var NewUrl=SearchUrl+value; 
    alert(NewUrl); // i am getting expected URL in alert. 
    window.location.replace(NewUrl); 
}); 

我在預警框中獲得預期的URL。但它不會重定向到新的URL。我甚至只用window.location但我得到了同樣的結果。有人可以幫我嗎?

+0

@Satpal我試過了,但得到了同樣的結果。 –

+0

控制檯是否顯示任何錯誤? – Script47

+0

@ Script47我在控制檯中沒有收到任何錯誤。只有頁面被刷新。它不會重定向到新的頁面...不會更改URL。 –

回答

1

你需要停止按鈕,這樣的形式不提交的默認操作,否則會以表格的網址,而不是的window.location.replace

$('.SearchBtnGo').click(function(e){ // add an e to this function argument 
    e.preventDefault(); // prevent the default action of the button 

    var url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl; 
    var value=$('#ctl00_ctl55_csr_sbox').val(); //Taking value from search box 
    var SearchUrl = url+"/_layouts/15/osssearchresults.aspx?k="; 
    var NewUrl=SearchUrl+value; 
    alert(NewUrl); // i am getting expected URL in alert. 
    window.location.replace(NewUrl); 
}); 
+0

我試過了,但在控制檯中出現錯誤,提示「Uncaught ReferenceError:e is not defined」。 –

+0

您是否也在函數中放入了'e' @DilipKumarYadav(請參閱上面代碼的前兩行) – Pete

+1

Yuppp @ pete.I已經忘記了這一點。但現在它工作。非常感謝。 –

1

我有網址這個問題,我通過使用window.open解決了這個問題,然後根據我想要點擊的按鈕來分配第二個參數。

window.open(NewUrl, '_self'); 

http://www.w3schools.com/tags/att_a_target.asp

+0

是的,它工作。但在新窗口中打開它。所以我使用e.preventDefault(),並且它按照需要工作。 –

+0

就像我說的,如果你使用上面的鏈接,你可以通過改變第二個參數來得到你想要的結果。 –

+0

是的,它按需要工作。 –

相關問題