1

Really simple question for you guys (not so simple for me)

I have put together a script based on other threads here on StackOverflow but I have unique need. There is a form on my site where users will submit it then it will add something like "?updated=555" to the url. However this number changes depending on what they updated.

I need a way to search for just the number (555 in my example) so I can scroll back to that div when the form is submitted and page reloads. Each div has a unique ID with a number that will match that query string when the url is submitted. So all I need is a way to find out what the number is after "?updated=" so I can pass it to this

scrollTop: $("#my-number").offset().top

Here is what I have so far:

<script>
jQuery(document).ready(function($) {
    if(window.location.href.indexOf("?updated=") > -1) {
       $('html, body').animate({
            scrollTop: $("#my-number").offset().top
        }, 2000);
    }
});
</script>

3 Answers 3

1

You can use regex to get the last number from your url:

var lastNumber = window.location.href.match(/\d+$/);

then use:

scrollTop: lastNumber
Sign up to request clarification or add additional context in comments.

Comments

1

See: https://stackoverflow.com/a/14663371/183181

function URLParameters(_querystring){
    var queryString = _querystring || window.location.search || '';
    var keyValPairs = [];
    var params      = {};

    queryString = queryString.replace(/^[^?]*\?/,''); // only get search path

    if (queryString.length)
    {
       keyValPairs = queryString.split('&');
       for (pairNum in keyValPairs)
       {
          if (! (!isNaN(parseFloat(pairNum)) && isFinite(pairNum)) ) continue;
          var key = keyValPairs[pairNum].split('=')[0];
          if (!key.length) continue;
          if (typeof params[key] === 'undefined')
             params[key] = [];
          params[key].push(keyValPairs[pairNum].split('=')[1]);
       }
    }
    return params;
}


var params = URLParameters();
params.updated; //[555]

So if you know it's only one value then, params.updated[0] or params.updated.shift() should equal 555

Comments

0

Instead of that, how about using bookmarks?

http://stackoverflow.com/url#updated555

<a id="updated555"></a>

otherwise...

var queries = location.query.split('&');
for(var i=0; i<queries.length; i++)
   if(queries[i].substr(0,8)==='updated=')
       $('html, body').animate({
         scrollTop: $("#"+queries[i].substr(8)).offset().top
       }, 2000);

...and you could still use bookmarks, since that's what they are for...

var query = location.hash;
if(query.substr(0,7)==='updated')
   $('html, body').animate({
     scrollTop: $("#"+query[i].substr(7)).offset().top
   }, 2000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.