Kevin
Kevin

Reputation: 1

saving scroll distance and passing it through the URL

I'm trying to create a link that when clicked is able to run a javascript function that counts how far down the page has been scrolled, then sends that value through the URL... i mostly work using php so im not very well versed n javascript

My Link

    <?php    
            echo '<a href="test.php?scroll=';
    ?>
            <script>
                   $(document).scrollTop();
            </script>
    <?php    
            echo '">add to cart
            </A>';
    ?>

Javascript function:

<script>
    $(document).scroll(function() {
        console.log($(document).scrollTop());
})

Upvotes: 0

Views: 326

Answers (2)

boisvert
boisvert

Reputation: 3729

You need to use javascript to update, dynamically, the data that you want to send when the user clicks. Samples here are HTML / javascript code, to give you the gist, but my JQuery is useless:

Using a form:

<form action="test.php" name="theForm">
   <input type="hidden" name="scroll" value="0" />
   <input type="button" value"Add to cart" />
</form>

<script>
$(document).scroll(
  function() {
    document.theForm.scroll.value = $(document).scrollTop();
  }
);
</script>

Scrolling triggers the JQuery event handler, which goes to the form and updates the value of the hidden field. When the user clicks add to cart, the scroll data, in the hidden field, is submitted.

Upvotes: 0

pderaaij
pderaaij

Reputation: 1337

Can't you just use link anchors?

http://www.w3.org/TR/html401/struct/links.html#h-12.2.3

Upvotes: 1

Related Questions