Search code examples
javascriptmobileswipe

How to follow a link via swipe in javascript?


To handle swiping I use the script posted here: http://padilicious.com/code/touchevents/

It works fine.

Now instead of changing the background (what the original script does), I would like it to grab the link contained within an <a> which has a class, and which is normally a link to the next page, but for mouse events like so:

<a href="mypage02.html" target="_self" class="NextP" title="My Second Page">

and then load the page.

I have many pages, with the same structure, I don't want to manually define the links. I want the js to get hold of the current href contained in the <a> and launch it, when triggered by the swipe. If possible.

Thank you ;-)


Solution

  • From what I understand, you want to look for a

    <a href="http://example.com/" class="NextP">
    

    element in a page (an <a> anchor tag with a NextP class), and when the user swipes, visit that link.

    To do this, I would

    1. look through your HTML for an a.NextP element, and capture its href attribute.
    2. when the user swipes, set window.location.href to this attribute.
    window.onload = function(){
      var nextPageUrl = document.querySelector('a.NextP').href;
      
      // just guessing how swiping works, I haven't looked through your library
      document.body.onswiperight = function(){
        window.location.href = nextPageUrl;
      };
    };
    

    Of course, you would use the correct method of detecting the swipe.