2014-12-08 55 views
0

我有同一页(或锚标记)链接在一个单一的html文件。当用户点击这些链接时,他们会在新标签页中打开相同的html文档。用新标题重新加载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> 
+0

您对网址不完全一样吗?像index.html和一个新的窗口打开可能是index.html?q = 1,你可以在你的javascript中进行读取以找到值q = 1解析1并根据该值改变标题。 – Khaltazar 2014-12-08 03:13:16

+0

是的,第一个标题将是“默认标题”,未来标题将类似于“文档”或“我刚刚点击过的页面的新部分” – ecoe 2014-12-13 03:09:58

回答

0

如果打开的新标签页是您制作的页面,请在页面加载时更改标题。您可以检查推荐人。 如果新标签是标题无法更改的外部页面,则可以简单地创建自己的页面,该页面显示iframe中的外部页面。

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

麻烦是新标签打开相同的页面 - 它是一个相对的,相同的页面链接。 – ecoe 2014-12-09 01:50:22

+0

那“麻烦”是怎么回事?没什么区别。 – Collector 2014-12-09 05:12:57

+0

我已经将标题定义为默认标题,那么如何更改它?当页面重新加载时,它使用相同的默认标题。 – ecoe 2014-12-13 03:07:32

1

有两种方法来完成这个 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>