SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.referrer.indexOf - how to select/click element in links drop down options

    Hi,

    I have the following code which individually works, but I don't know how to put it together in JavaScript......


    if document.referrer.indexOf('Belgium')>0



    then ("a.optionLink:contains('Belgium"+"')").click();



    else ("a.optionLink:contains('England"+"')").click();



    Any chance you could show me how to script this??

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,271
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    Hi Stevvo,

    Is this what you're after?
    Code JavaScript:
    if (document.referrer.indexOf('Belgium') > 0) {
        $("a.optionLink:contains('Belgium"+"')").click();
    } else {
        $("a.optionLink:contains('England"+"')").click();
    }

  3. #3
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Youre a genius!!!! thanks!!!

    If I want to add a few more options, eg France, Spain, Brazil is that possible???

    thanks very much fretburner

  4. #4
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,271
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    What is it that your code does? If it finds the name of the country in the url, it clicks on a link, but what does that link do? I only ask because there might be an easier way depending on what you're trying to achieve.

  5. #5
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    when you click onto the search page from a country page the code automatically picks up the country from the last URL then automatically selects that country from the drop down option link list, rather than you manually selecting it once on the search page. I've got a whole load of countries to add, but I've noticed that it may need an on load first because although the country is now selected it has not changed the search results from the default provision......!!

    Does that help? I'm very new to this.

    Thanks for your help.

  6. #6
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,271
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    It sounds like you might be better off passing the country in the URL (e.g. example.com/search?country=Belgium). That way you can render the page with the correct results without needing any JS, and it would also allow the user to bookmark the link and come back to it later.

  7. #7
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, cool, will look at that.
    Thanks for your help.

    Stevvo.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •