【问题标题】:Check if no hashtag is present, hashtag undeclared, no hash检查是否没有标签,标签未声明,没有哈希
【发布时间】:2012-06-19 15:13:57
【问题描述】:

我正在开发这个网站: http://parkoura.tk/a/sandbox/

我正在使用 ajax 导航。我有一个名为 ajax() 的函数,它应该在加载时执行。当它出现时,我希望它首先检查是否存在主题标签。如果没有主题标签,则该函数应该只是中断而不做任何事情。如果声明了主题标签,它会将 ajax 发送到另一个 php 并替换 div 内的内容。

除了 onload 哈希标签检查部分外,它还可以完美运行。当网站加载时,您可以看到一个滑块被渲染了一会儿。尽管没有声明主题标签,但它仍然会继续替换内容。

我有提醒显示逻辑应该如何工作。我实际检查的逻辑是这样的:

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

完整的js代码: https://pastee.org/v8cxp

【问题讨论】:

    标签: ajax onload hashchange


    【解决方案1】:

    试试这个:

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

    可能重复:How can you check for a #hash in a URL using JavaScript?

    【讨论】:

    • @user1392515 关于您的代码 sn-p,您可以添加 if 语法:&amp;&amp; window.location.hash != '#carousel'
    猜你喜欢
    • 1970-01-01
    • 2018-03-03
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多