Zayd Bhyat
Zayd Bhyat

Reputation: 101

Navigating to different pages on single page template

I have a one page template that i am trying to modify so that on clicking particular links the user goes to a new page rather than scroll to a different section. I still want to keep the scroll to a section navigation but I have call to action buttons in those sections that need to go to different pages. The website is a wordpress site

Here is my js code:

$(document).ready(function() {
if ($("a" != ".external")){
        $("a").on('click', function(event) {

            if ((this.hash !== "/membership-form/") && (this.hash !== "#wood-grain") && (this.hash !== "#gifting") && (this.hash !== "#non-slip") && (this.hash !== "#wpcf7-f4-o1") && (this.hash !== "#carousel-control-next") && (this.hash !== "#carousel-control-prev") && (this.hash !== "#carouselExampleControls")) {

                event.preventDefault();

                var hash = this.hash;

                $('html, body').animate({

                    scrollTop: $(hash).offset().top - 100

                }, 800, function() {});

            }

        });
    }

});

So on this line if ($("a" != ".external")) I added a class called external to the <a> tags that need to leave the page. I also tried this.hash !== "/membership-form/" to try and target particular <a> tags but it only seems to work with my forms and image gallery that have # references. How can I get the site to navigate away from the home page to my other pages? Thanks

UPDATED JS CODE

AOS.init();
var $ = jQuery;
$(document).ready(function() {
    $("a").on('click', function(event) {
        if (!($(location).attr('href', '/BDWest/membership-form/'))){
            if ((this.hash !== "/membership-form/") && (this.hash !== "#wood-grain") && (this.hash !== "#gifting") && (this.hash !== "#non-slip") && (this.hash !== "#wpcf7-f4-o1") && (this.hash !== "#carousel-control-next") && (this.hash !== "#carousel-control-prev") && (this.hash !== "#carouselExampleControls")) {
                event.preventDefault();
                var hash = this.hash;
                $('html, body').animate({
                    scrollTop: $(hash).offset().top - 100
                }, 800, function() {});
            }
        }   
    });
});

UPDATED WITH THE PHP CODE HEADER.php

<!DOCTYPE html>
<html lang="en">
    <head>    
        <meta charset="utf-8">    
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">    
        <meta name="description" content="">    
        <meta name="author" content="">    
        <title><?php bloginfo('name');?> | <?php wp_title(); ?></title> 
        <link rel="pingback" href="<?php bloginfo('pingback_url');?>" />    
        <!-- Bootstrap core CSS -->  
        <?php wp_head(); ?>    
    </head>  
    <body <?php body_class(); ?>>  
        <div class="loader">
            <img alt="first image" src="<?php print IMAGES;?>/3.gif">  
        </div> 
        <div class ="parallax-full" id="soon">              
        <div class="banner-center">             
            <div class="sticky-top">                    
                <img src="https://via.placeholder.com/468x60?text=Advert" class="img-fluid"/>               
            </div>          
        </div>
            <div class="hero-textBG">       
                <div class="logo">          
                    <h1>CLUB BD WEST</h1>
                    <h2>Logo can be here</h2>                   
                    <a class="external" href="/membership-form/"><button type="button" class="btn btn-default">Register Today</button></a>

                </div>      
                <div data-aos="fade-up" data-aos-offset="0">            
                    <div class="scroll-button">             
                        <a href="#home">
                            <img class="img-fluid" alt="scroll-button" src="<?php print IMAGES;?>/button-down.png">
                        </a>                
                        <h6>Scroll down</h6>
                    </div>      
                </div>      
            </div>
        </div>

navigation.php

  <!-- Navigation -->
<nav class="navbar sticky-top navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#home">       <img src="https://via.placeholder.com/70x40?text=Very+Small+Logo" class="img-fluid"/>   </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#home">Home
            <span class="sr-only">(current)</span>
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#section2">Our Story</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#section3">Events</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#section4">Gallery</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#section5">Contact Us</a>
        </li>
      </ul>
    </div>
  </div>
    </nav>

index.php

<!-- Header --><?php get_header(); ?><!--Menu -->
<?php get_template_part('navigation');?><!--Home Section -->
<div class ="parallax" id="BG-2">
    <div class="container-fluid">
        <div id="home">
            <div class="row">
                <div class="col-sm-12 text-center">
                    <div class="content">
                        <div class="mt-2 col-md-12">
                            <div data-aos="fade-up">
                                <h1 class="mt-1">CLUB BD WEST</h1>
                                <p class="lead">There are no winners or losers, ours is a philosophy of enjoyment through participation and we have organized a range of activities to suit all ages, fitness levels and activity choices.</p>                          
                            </div>        
                        </div>
                    </div>        
                </div>
            </div>
        <div class="w-100">     </div>
            <section class="top-section">
                <div data-aos="fade-up">
                    <div class="container">
                        <div class="row">
                            <div class="col text-center">
                                <img class="icon" src="<?php PRINT IMAGES;?>/weights-icon.png"></img>
                                <h3>Badminton</h3>
                            </div>
                            <div class="col text-center">
                                <img class="icon" src="<?php PRINT IMAGES;?>/staff-icon.png"></img>
                                <h3>Football</h3>
                            </div>
                            <div class="col text-center">
                                <img class="icon" src="<?php PRINT IMAGES;?>/aerobics-icon.png"></img>
                                <h3>Cricket</h3>
                            </div>
                            <div class="col text-center">
                                <img class="icon" src="<?php PRINT IMAGES;?>/spinning-icon.png"></img>
                                <h3>Recreational Cycling</h3>
                            </div>
                            <div class="col text-center">                               
                                <img class="icon" src="<?php PRINT IMAGES;?>/aerobics-icon.png"></img>                              
                                <h3>Kayaking</h3>           
                            </div>                          
                            <div class="col text-center">
                                <img class="icon" src="<?php PRINT IMAGES;?>/spinning-icon.png"></img>                              
                                <h3>Nature rambling</h3>                            
                            </div>
                        </div>                      
                        <div class="row">                           
                            <div class="col text-center">                               
                                <img src="https://via.placeholder.com/500x60?text=Call+to+action+Club+Sports" class="img-fluid"/>                           
                            </div>                      
                        </div>
                    </div>                  
        </div>          
            <div class="row">       
            <div class="col-sm-12 text-center">         
                <div class="content">               
                <div class="mt-2 col-md-12">                    
                <div data-aos="fade-up">                        
                    <p class="lead">The list of activities is growing, fueled by suggestions from our members.  It’s your club and responds to your needs..</p>                 
                </div>                      
                </div>          
                </div>              
                </div>  
                </div>                      
                <div class="container">                     
                <div class="row">                           
                <div class="col text-center">                               
                <h3>Our Goals</h3>                              
                <p>To build an online social platform bringing together a diversity of people sharing a common purpose of interaction through sport and recreation.</p>                         
                </div>                          
                <div class="col text-center">                               
                <h3>Our Mission</h3>                                
                <p>To continue growth of both membership and activities developing a multicultural community with a social conscience. </p>                         
                </div>                          
                <div class="col text-center">                               
                <h3>Our Vision</h3>                             
                <p>To add to the quality of life, build cohesion in communities and enrich society.</p>                         
                </div>                      
                </div>                  
                </div>      
</section>
</div>
    </div><!-- Container End -->
<hr class="content-seperator"></hr>
    <div id="section2">
        <div data-aos="fade-up">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12 text-center">
                        <h1>Our Story</h1>
                        <p>It all started when a group of friends got together for an impromptu game of badminton. The shared love of the game and the company of friends was a compelling mix and very quickly these random get togethers developed into a regular event.</p>                      <p>The group began to grow rapidly, first from within the circle of friends and later extending to others that were exposed to the group and drawn to it.  At this point it was evident that a structure was required to communicate with group members and so BD West was formed.</p>                      <p>BD West is now a not for profit social club with over 200 members and plans to grow into a multicultural community of like-minded people brought together by the love of sport, recreation and the harmony of social connection. </p>
                    </div>
                </div> 
            <br>
                <div class="row">
                    <div class="col-sm-12 text-center">
                        <h2>Our Management Team</h2>
                    </div>
                    <div class="col-sm-12 text-center">
                        <img src="https://via.placeholder.com/500x60?text=Call+to+action+Management+Team" class="img-fluid"/>
                    </div>
                </div>
            </div>
        </div>
    </div>
<hr class="content-seperator"></hr>
</div><!-- BG End -->
<div class ="parallax" id="BG-3">       <div class="col-sm text-center">            <img src="https://via.placeholder.com/468x60?text=Advert" class="img-fluid"/>       </div>
    <div id="section3">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 mb-sm-0 text-center">
                    <div class="content">
                        <div data-aos="fade-up">
                            <h3 class="mt-2">Events</h3>
                            <p class="lead">Here are some of our latest events</p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12 text-center">
                </div>      
            </div>
        </div><!--./container -->
    </div>  <!-- ./section3 -->
<hr class="content-seperator"></hr>
</div><!--./ BG3 -->            <!-- Pricing Table -->
<div class ="parallax" id="BG-1">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 mb-3 mb-sm-0 text-center">
                <div id="section4">
                    <div class="content">
                        <div data-aos="fade-up">
                            <h3 class="mt-2">Gallery</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<hr class="content-seperator-yellow"></hr>
</div>        <!-- Contact Form and Location -->            
<div class ="parallax" id="BG-2">
    <div id="section5">
        <div class="container">
            <div class="row">
                <div class="col-12 text-center">
                    <div data-aos="fade-up">
                        <div class="content">
                            <h3 class="mt-2">Contact Us</h3>
                        </div>
                    </div>  
                </div>
            </div>
            <div class="row">
                <div class="col-12 text-center">
                    <div data-aos="fade-up">
                        <?php echo do_shortcode('[contact-form-7 id="16" title="Contact Us"]');?>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12 text-center">
                    <div data-aos="fade-up">
                        <p class="lead">BD West Incorporated</p>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12 text-center">
                    <div data-aos="fade-up">
                        <div class="content">
                        </div>  
                    </div>
                </div>  
            </div>              
        </div>
    </div>
</div>
<!--Footer Section -->
<?php get_footer();?>

Upvotes: 1

Views: 389

Answers (1)

R. Srour
R. Srour

Reputation: 160

if you want to link through jQuery it would be like this:

$(location).attr('href', 'http://stackoverflow.com')

But it is highly recommended to do this with pure Javascript it best simulates an HTTP redirect, and it would go like this:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

It's better to use window.location.replace(...), because the replace() doesn't keep the page they'd come from in the session history, which avoids having the users stuck in a never-ending back-button loop.

Upvotes: 1

Related Questions