You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过window.location.href判断首页并控制注册弹窗显示

Got it, let's work through this problem to get your conditional modal trigger set up correctly!

Here's the Implementation

First, we need to make sure our code runs only after the page's DOM is fully loaded (since we're using jQuery to interact with the modal element). Then we'll check if we're on the homepage using window.location.href, and trigger the modal after 3 seconds if we are.

// Wait for the DOM to be ready before running our code
$(document).ready(function() {
    // Check if we're on the homepage by comparing the full URL to '/'
    if (window.location.href === '/') {
        // Trigger the register modal after a 3-second delay
        setTimeout(function() {
            $("#modalregister").modal('show');
        }, 3000);
    }
});

Quick Breakdown of the Logic

  • DOM Ready Wrapper: Using $(document).ready() ensures that the #modalregister element exists in the DOM before we try to show it—this prevents "element not found" errors.
  • Homepage Check: The condition window.location.href === '/' directly matches your requirement to use this exact check. Note: If your homepage URL might include the full domain (like https://your-site.com/), you could swap this to window.location.pathname === '/' for a more reliable path-only check, but we've stuck with your specified method here.
  • Non-Homepage Behavior: For all other pages, you don't need any additional code! Your existing REGISTER link <a href="#" data-toggle="modal" data-target="#modalregister" class="signup_btn">REGISTER</a> will work perfectly as-is—Bootstrap's built-in data-toggle and data-target attributes handle triggering the modal when the link is clicked, which aligns with your requirement to only allow this click-based trigger on non-homepages.

内容的提问来源于stack exchange,提问作者B L Praveen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:46:17