0

我在 Silverlight 应用程序中有此代码default.aspx

<head id="Head1" runat="server">   
  <link id="link1" rel="shortcut icon" href="favicon1.ico" type="image/vnd.microsoft.icon" />
  <title id="Title1" >hello</title>

更改查询字符串时,我需要更改href="favicon2.ico"JavaScript 中的。

我试图写这个,但它不工作:

<script type="text/javascript">
  if( Request.QueryString ("user"). Equals("true", StringComparison.OrdinalIgnoreCase)) {
    document.title="hi user"; 
    document.getElementById('link1').href = ('favicon2.ico');
  }
</script>
</head>
</html>
4

2 回答 2

0

尝试在您的代码中替换它

document.getElementById('link1').href = "favicon2.ico";
于 2013-06-11T11:17:11.297 回答
0

添加 JavaScript 函数

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

将您的代码更改为纯 JavaScript(您正在混合 C# 和 JavaScript)

if(getParameterByName("user").toLowerCase() == "true")
{
   document.title="hi user"; 

   var newImage = new Image();
   newImage.src = 'favicon2.ico?_=' + new Date().getTime();
   document.getElementById('link1').href = newImage.src;    
}

更新:

if(getParameterByName("user").toLowerCase() == "true")
{
   document.title="hi user"; 
   changeFavicon('favicon2.ico');
}

function changeFavicon(src) {
var link = document.createElement('link'),
oldLink = document.getElementById('link1');
link.id = 'link1';
link.rel = 'shortcut icon';
link.href = src;
if (oldLink) {
    document.getElementsByTagName('head')[0].removeChild(oldLink);
}
document.getElementsByTagName('head')[0].appendChild(link);
}
于 2013-06-11T11:22:28.160 回答