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!
}
精彩评论