开发者

Check URL for #hash tag

开发者 https://www.devze.com 2023-03-07 05:24 出处:网络
I need to detect once by load if a hash with #comment or #map is in the url to open a spoiled div with the comments and map.

I need to detect once by load if a hash with #comment or #map is in the url to open a spoiled div with the comments and map. This is the JavaScript that I use to open and close a div:

<script type="text/javascript">
function spoilinout(idinout){
if (document.getElementById) {
var dividinout = document.getElementById(idinout);
dividinout.style.display = (dividinout.style.display=='block'?'none':'block');
} }
</script>

How can I detect i开发者_JAVA技巧t? Thanks Frank


The current URL is stored in window.location.href so you can always test:

if (window.location.href.match(/\#comment/))
{
   // ...
}


Use location.hash

if (/#comment|#map/i.test(location.hash)) { /* do your thing */}


window.location.hash should give you that hash fragment back.

From there, you can switch on the hash fragment:

switch(window.location.hash){
    case "#comment": 
        // comment!
    case "#map":
        // map!
}
0

精彩评论

暂无评论...
验证码 换一张
取 消