简体   繁体   English

刷新时如何更改网页的标题?

[英]How can I have the title of a webpage change when it is refreshed?

I was wondering if there is a simple way of having the title of a webpage change between some chosen presets stored as a list somewhere each time the page is loaded/refreshed?我想知道是否有一种简单的方法可以在每次加载/刷新页面时将网页标题在某些选定的预设之间更改为列表?

For example Wowhead where the title changes each time the page is loaded.例如Wowhead每次加载页面时标题都会更改。

I've been looking for a while but can't find any tutorials or anything about it.我一直在寻找一段时间,但找不到任何教程或任何关于它的东西。 Any help at all would be helpful.任何帮助都会有所帮助。

EDIT: Thanks for your help you guys.编辑:感谢你们的帮助。 I'm not very good at this kind of stuff but I eventually managed to get it to work after using some of your advice to keep searching.我不太擅长这类东西,但在使用您的一些建议继续搜索后,我最终设法让它工作。

I eventually got it going with:我最终得到了它:

public string randomTitle()
{
    Random random = new Random();
    var page = (Page)HttpContext.Current.Handler;
    int randomNumber = random.Next(0, 10);
    string response;
    switch (randomNumber)
    {
        case 0:
            response = "someTitle0";
            break;
        case 1:
            response = "someTitle1";
            break;
        case 2:
            response = "someTitle2";
            break;
        //I had more in here but you get the picture
    }
    return response;
}

and on the page I called it with:在页面上我用以下命令调用它:

  <title><%=randomTitle()%></title>

Thanks for all of your help everyone who responded.感谢所有回复的人的帮助。 I probably wouldn't have been able to do it if you hadn't (I did spend a good while searching before asking, I just didn't know what to search.)如果你不这样做,我可能无法做到(我在询问之前确实花了很多时间搜索,我只是不知道要搜索什么。)

I have checked http://www.wowhead.com/ and found that everytime when page refreshed it just randomly updated the page title.Below example used javascript to set page title.I suggest to use server side script for the same.我检查了http://www.wowhead.com/ ,发现每次刷新页面时它只是随机更新页面标题。下面的示例使用 javascript 设置页面标题。我建议使用服务器端脚本。

<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type='text/javascript'>


$(document).ready(function(){

var randomno=Math.floor((Math.random()*10)+1);
var pagetitle='This is Page Title';
if(randomno==1)
{   
    pagetitle='Title-1';
}
else if(randomno==3)
{
    pagetitle='Title-3';
}
else if(randomno==5)
{
    pagetitle='Title-5';
}
else if(randomno==2)
{
    pagetitle='Title-2';
}
else if(randomno==7)
{
    pagetitle='Title-7';
}
else
{
pagetitle='Default page title';
}
$(document).attr('title', pagetitle);
});
</script>
</head>
<body>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM