最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - if window.location.href has 'sometext' inside - do this - Stack Overflow

programmeradmin2浏览0评论

Currently working on something which uses ajax for some pagination. What I'm looking to do is add something like referal=3 to the end of some links then when they go to that link I'll insert a back button with the window location for example:

User uses the ajax pagination, goes to page 3 I'll add ?ref=3 to the end of the link or something so it would like this this:

/?ref=3

(not sure if that's a corrent way of doing it)

Then the user clicks that link and I'll append a back button on that page with that link.

So when they go back to the homepage using the generated button the window.location.href will be:

The idea is that when they click that back button, the ajax pagination will load to page 3.

I'm not sure how else to explain this, but does anyone know how I would go about doing a conditional statement dependant on the window.location.href?

Currently working on something which uses ajax for some pagination. What I'm looking to do is add something like referal=3 to the end of some links then when they go to that link I'll insert a back button with the window location for example:

User uses the ajax pagination, goes to page 3 I'll add ?ref=3 to the end of the link or something so it would like this this:

http://foo./somepage/?ref=3

(not sure if that's a corrent way of doing it)

Then the user clicks that link and I'll append a back button on that page with that link.

So when they go back to the homepage using the generated button the window.location.href will be:

http://foo.?ref=3

The idea is that when they click that back button, the ajax pagination will load to page 3.

I'm not sure how else to explain this, but does anyone know how I would go about doing a conditional statement dependant on the window.location.href?

Share Improve this question asked Jun 2, 2011 at 12:20 daryldaryl 15.3k21 gold badges70 silver badges93 bronze badges 7
  • Well one problem is that there's not much you can do to prevent people from clicking the real "Back" button on their browser ... – Pointy Commented Jun 2, 2011 at 12:24
  • @Pointy Well yeah I know, but this has nothing to do with the user clicking the real back button. – daryl Commented Jun 2, 2011 at 12:25
  • It seems you are trying to track users' click flow through your site, is that correct? – Stephen Commented Jun 2, 2011 at 12:26
  • @Stephen - I just want to improve the functionality of the site. I know it's not deemed as a necessary thing to have since every browser has a back button. But that back button bees redundant when you have ajax'd pagination. So i was just thinking of offering an alternative user friendly option. – daryl Commented Jun 2, 2011 at 12:28
  • Not really sure if I understood what you are after, but something similar to if (document.location.href.search("ref=3")!=-1){ alert('got ref=3'); }? – Niklas Commented Jun 2, 2011 at 12:30
 |  Show 2 more ments

3 Answers 3

Reset to default 3

To find a if the url contains some string:

if (document.location.href.search("ref=3")!=-1){
 alert('got ref=3'); 
}

You can use this function to get parts of the query string:

function getQueryString(variable){
    // Grab the query string part of the URL (everything after the ?)
    var query = window.location.search.substring(1);

    // If you don't specify which value you want, return the whole thing
    if(!variable) return query;

    // Split the query string to key=value pairs into an array
    var vars = query.split('&');

    // Loop through them to find the one we're looking for
    for (var i = 0; i < vars.length; i++){
        // Split key and value
        var pair = vars[i].split('=');

        // If the key matches our parameter, return the value
        if (pair[0] == variable){
            return pair[1];
        }
    }
    // If not found, return empty string
    return '';
}

Then you can get the value as a string by:

var value = getQueryString('ref');

// Conditional code if it is a certain value, do something
if(value === '3'){
    // Do something
}

You can also get it as a number by using parseInt():

var value = parseInt(getQueryString('ref'), 10);

if(value === 3){
    // Do something
}

I'm not sure I fully understand what you want to do, but it sounds like you want to look into using the location.hash property.

Using a jQuery plugin, such as hashchange, you can add ajax calls to the browsers history. Also, all pages will be bookmarkable. It's a pretty broad subject, but the link I provided should get you started.

发布评论

评论列表(0)

  1. 暂无评论