0

I'm trying to add a variable to the end of my href using regex and jquery. This is what i have so far:

$('#survey-click1').click(function(event) {
      playerPause();
      $("a[href^=http://d.surveysonline.com/]")
          .each(function()
      {
        this.href = [regex goes here];
      });
      return false;
});

I'm simply trying to add a variable at the end of the url i clicked on to attach whether or not the video i just watched had a preroll or not. This could be done easily by attaching this to the end of my url code [&preroll="+Kdp3State.preroll+"]. How do i detect the end of the href string to attach this to to it?

Thanks

3 Answers 3

1

Assuming that this is actually what you want to append to your URL, this should work:

$("a[href^=http://d.surveysonline.com/]").each(function() {
    $(this).attr('href', $(this).attr('href', '&preroll='+Kdp3State.preroll);
});

Apart from the fact that you use a CSS selector that is somewhat similiar to a regex, this question seems be unrelated to the subject.

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

Comments

1

There's no need for a regexp, just do:

this.href += 'string_to_be_appended';

e.g.

this.href += '&preroll=' + encodeURIComponent(Kdp3State.preroll);

Note the use of encodeURIComponent to ensure that any special characters (+, %, etc) in the resulting URI are correctly encoded.

5 Comments

Judging from his code, he is using jQuery. this.href would be invalid in that case. Instead, use this.attr('href')
actually, it looks like I was right. $(selector).each() supplies the underlying elements in this, not a jQuery object.
Oh, nice :) I didn't actually know that. We learn something new every day, I guess. (still seems pretty odd though)
that's why I hang around here - sometimes you learn a lot from ones own incorrect answers :)
FWIW, from api.jquery.com/each - "the callback is fired in the context of the current DOM element, so the keyword this refers to the element"
0
this.href += "&preroll=" + Kdp3State.preroll

or

this.href = this.href.replace(/&preroll=[^&]+/g, "") + "&preroll=" + Kdp3State.preroll;

?

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.