Search code examples
ajaxonloadhashchange

Check if no hashtag is present, hashtag undeclared, no hash


I'm developing this website: http://parkoura.tk/a/sandbox/

I am using an ajax navigation. I have a function called ajax() which is supposed to execute at onload. When it does, I would like it to check first if a hashtag is there. If there is no hashtag, the function then should just break and not do anything. If a hashtag is declared, it sends ajax to another php and replaces content inside a div.

It works perfectly besides the onload hash tag checking part. When the site loads up, you can see for a moment that a slider is rendered. Despite the fact that no hashtag is declared, it then still goes on to replace the content.

I have alerts to show how the logic should be working. My logic for the actual checking is this:

    <script>
    if (typeof location.hash != 'undefined' || location.hash != '' || location.hash != '#' || location.hash != '#carousel')
    { no hash,things stay the same the function is not executed. 

full js code: https://pastee.org/v8cxp


Solution

  • Try this :

    if(window.location.hash) {
        // Process your ajax call
    } else {
        // "no hash,things stay the same the function is not executed. "
    }
    

    Possible duplicate of : How can you check for a #hash in a URL using JavaScript?