簡體   English   中英

重新載入具有新標題的html頁面

[英]Reload html page with new title

我在單個html文檔中具有相同的頁面 (或anchor tag )鏈接。 當用戶單擊這些鏈接時,他們會在新選項卡中打開相同的html文檔。

該新標簽頁如何擁有新的文檔標題?

我下面的代碼的問題在於它重命名了當前選項卡的標題(而不是剛剛打開的新標題)。 首次加載頁面時,我要為該頁面提供一個默認標題。 當然,所有隨后加載的新頁面都將其設置為此標題。

<title>Default title</title>
<a href=#relativeLink target='_blank' onclick="return runMyFunction();">click me</a>

<script>
    function runMyFunction() {
        document.title="new title!"
        return true;
    }
</script>

有兩種方法可以實現此目的:1,將標題作為Url參數傳遞; 2,將所有標題保存在數組中,以及將所選標題傳遞給標題數組

<html>
  <head>
    <title>Default title</title>
  </head>
<body>
    <a href="?newTitle=My new title" target="_blank" >newTitle equal to My title 1</a>
    <a href="?newTitle=My new title 2" target="_blank" >newTitle equal to My title 2</a>
    <a href="?newTitle=My new title3" target="_blank" >newTitle equal to My title 3</a>
    <br />
    <a href="?tIdx=0" target="_blank" >tIdx equal to 0</a>
    <a href="?tIdx=1" target="_blank" >tIdx equal to 1</a>
    <a href="?tIdx=3" target="_blank" >tIdx equal to 2</a>
    <script>

        var titles = ["title 1", "title 2", "title 3" , "title 4"];


        function getUrlParameterValueByName(name) {
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(window.location.search);
            if (results == null)
                return "";
            else
                return decodeURIComponent(results[1].replace(/\+/g, " "));
        }

        var newTitleValue = getUrlParameterValueByName('newTitle');
        var titleIndex = getUrlParameterValueByName('tIdx');
        if (newTitleValue)
            document.title = newTitleValue;
        if (titleIndex)
            document.title = titles[titleIndex];


    </script>
</body>
<html>

如果打開的新標簽頁是您創建的頁面,請在加載頁面時在其中更改標題。 您可以檢查引薦來源網址。 如果新標簽頁是您無法更改其標題的外部頁面,則只需創建自己的頁面即可在iframe中顯示該外部頁面。

<title>My Title</title>
<iframe src="/path/to/external" />

暫無
暫無

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

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