最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery hash if no hash - Stack Overflow

programmeradmin6浏览0评论

Trying to get #home to display if there is no hash present in the url. I figured something along these lines would work pretty easy but I can't get anything going:

   if(window.location.hash != null){ 
      $(window.location.hash).fadeIn(800);
   } else {
      $('#home').fadeIn(800);
   }

I've never worked with if / else statements in jquery, so this is obviously wrong

thanks!

Trying to get #home to display if there is no hash present in the url. I figured something along these lines would work pretty easy but I can't get anything going:

   if(window.location.hash != null){ 
      $(window.location.hash).fadeIn(800);
   } else {
      $('#home').fadeIn(800);
   }

I've never worked with if / else statements in jquery, so this is obviously wrong

thanks!

Share Improve this question edited Jan 13, 2011 at 23:41 BoltClock 724k165 gold badges1.4k silver badges1.4k bronze badges asked Jan 13, 2011 at 23:38 ThomasThomas 1,0794 gold badges18 silver badges40 bronze badges 3
  • will window.location.hash give null? Maybe you should check for that first in some JS API. I think it will give you an empty string. – Marnix Commented Jan 13, 2011 at 23:42
  • @Marnix, You're correct. At least in Firefox it will give an empty string. – Highway of Life Commented Jan 13, 2011 at 23:50
  • that's exactly what it was, so i changed to empty and works perfect! – Thomas Commented Jan 14, 2011 at 0:02
Add a comment  | 

1 Answer 1

Reset to default 22

Compare it against the empty string instead (null and the empty string aren't equal in JavaScript):

if(window.location.hash != ''){ 
   $(window.location.hash).fadeIn(800);
} else {
   $('#home').fadeIn(800);
}
发布评论

评论列表(0)

  1. 暂无评论