簡體   English   中英

JS不同類型的重定向

[英]JS Different Kind of Redirection

我需要使用JS重定向頁面,我想知道如何做到這一點? 假設我已經在瀏覽器中打開了以下URL,其錨點標簽很少,

file:///someDrive:/someFolder/index.html

其中一個錨標簽已將onclick設置為redirect('login.html'); 當我按它它應該重定向到,

file:///someDrive:/someFolder/login.html

怎么樣?

<script type="text/javascript">
function redirect(page){
window.location = page;
}
</script>

<a onclick="redirect('http://www.google.com');"></a>

您可能需要將window.location更改為window.location.href以支持其他一些瀏覽器。

如果你真的想要一個JavaScript函數,你可以這樣做:

function redirect(page) {
    var href = window.location.href;
    // this will remove "/index.html" from the URL
    var path = href.substring(0, href.lastIndexOf('/'));

    window.location.href = path + '/' + page;
    // or window.location.assign(path + '/' + page);
}

不過,對於普通鏈接,您不需要這樣做,只需設置正確的href屬性即可。 另外,如果你沒有表格,我不會使用<input type="button" />而只是<button> (docu)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM