0

Ok, I'm new to JavaScript and need some help here. Working on forward/backward buttons on a page. Essentially, the url looks like this http://webaddress.com/details?id=27

So, my two functions are meant to extract the url and forward to the same page decrementing or incrementing the details id as necessary when one of the buttons is clicked. For example when someone clicks on the next button, id=28 is shown; and if previous is clicked, id=26 is shown.

I think it's something like substring, I've seen it done somewhere before, but not sure how's done.

I need idea on how to approach it. Any help will be appreciated.

Update: I was using @gion_13's code below, but looks like I'm missin something. It doesn't seem to work. Any other help will be appreciated.

6 Answers 6

2

You can use location.href to get or set the URL.

http://developer.mozilla.org/en/window.location

Use exec to extract the identifier.

extracted_id = /id=([0-9]+)$/.exec(location.href)[1]

http://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec

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

2 Comments

Thanks for this tip, but looks like your approach assumes the user is on a Mozilla browser.
Mozilla just hosts it - it's not browser specific (unless noted).
1
  1. Read the url as var url = document.location.href
  2. Use regEx or indexOf "=" or document.location.search to find current page no
  3. Parse as int and +/- page number
  4. rebuild the the url
  5. document.location.replace(newurl)

2 Comments

I was going to answer that but you beat me by 2 minutes! ;)
NP, try next time and be faster :)
0
function parseUrl(){
    if(location.href.indexOf('?') == -1)
        return null;
    var aux = location.href.split('?');
     var result = {
        'href' : aux[0],
        'params' : {}
    };
    var p = aux[1].split('&');
    for(var i=0,l=p.length;i++)
        {
             var kv = p[i].split('=');
             params[kv[0]] = kv[1];
        }        
    return result;
}

function reconstructUrl(obj){
    if(!obj)
        return location;
    var url = obj.href + '?';
    for(var i in obj.params)
        url += i + '=' + obj.params[i] + '&';
    return encodeURI(url);
}

function getNextUrl(){
    var urlObj = parseUrl();
    urlObj.id++;
    return reconstructUrl(urlObj);
}

function getPrevUrl(){
    var urlObj = parseUrl();
    urlObj.id--;
    return reconstructUrl(urlObj);
}

3 Comments

Thanks a million for this code. I appreciate it -I just added <input type="submit" onclick="getPrevUrl()" value="Previous" /> and nothing worked. Is there something I'm missing. Thanks again for your help.
Although the code did not work for me, it gave my something to think about and adapt for what I need it. Thanks again.
well, I haven't tested it, but the main idea is there. In order for it to work in your case, you should've written something like this : <input type="submit" onclick="window.location = getPrevUrl();" value="Previous" />
0

The window.location object has the URL info you need. You can parse it using a regex. Here's some references:

2 Comments

window.location doesn't return the GET parameters. You need to use document.URL
You have to use the whole location object. window.location.search has the query parameters.
0

To access the URL of the page, use document.URL, for example:

document.write(document.URL);

You can then modify the URL and open the new URL.

For lots of hints and tips, See JavaScript HTML DOM Examples.

Comments

0

You can do that without Javascript if you want. I think it's more efficient with PHP, because it will work even if some user has Javascript disabled.

You can try get the id like this:

<?php $my_id = $_GET['id']; ?>

1 Comment

Thanks, but did you read the question? Your code looks rather trivial.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.