1

So far I have done

jQuery:

function addURL(element)
{
var baseZipCode = 48180;
$(element).attr('href', function() {
    return this.href + baseZipCode;
});
}

html:

<a onclick="addURL(this)" href="http://www.weather.com/weather/today/" target="_blank">Click this</a>

My problem is when user clicks on link and the new window open everything is ok, but when the user clicks on the link again without a refresh, the variable is added twice to the link.

For example:

First time: http://www.weather.com/weather/today/48180

Second time: http://www.weather.com/weather/today/4818048180

It doesn't act right until you do a page refresh, any help would be appreciated. Thanks in advance

4 Answers 4

1

Replace your addURL function with

function addURL(element)
{
    var baseZipCode = '48180';
    if (element.href.slice(-baseZipCode.length) !== baseZipCode){
        element.href += baseZipCode;
    }
}

there is no jQuery involved..

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

2 Comments

+1 Nicely done, jQuery not needed. Is it better to check for existence first then add or just add then replace duplicates (lazy)?
@iambriansreed, more of a preference.. the only benefit to checking is that you can specify where to check, whereas just replacing the duplicate could (very unlikely in this scenario) replace another match in the original string..
1

You can try it with jQuery.fn.one

Javascript:

  jQuery("a").one("click", function(){ // Set a handler that execute once
        var baseZipCode = 48180;
        this.href += baseZipCode; //same as "this.href = this.href + baseZipCode"
    });

HTML:

<a href="http://www.weather.com/weather/today/" target="_blank">Click this</a>

Maybe you will need to add some class to <a> tags to differ it from another ones

2 Comments

+1 Not sure if the answer applies but jQuery.fn.one is a new one to me.
Sometimes jQuery.fn.one is very useful.. Instead of test each user click you will just execute handler on first click.. I think it is a good solution in this case..
0

This should be:

function addURL(element)
{
    var baseZipCode = 48180;
    element.href = (element.href + baseZipCode)
        .replace(baseZipCode + baseZipCode, baseZipCode);
}

Comments

0
return this.href.indexOf(baseZipCode) != -1 ? this.href : this.href + baseZipCode;

5 Comments

Guess this is not the case but if baseZipCode is already contained in the link it would fail. You will be better using slice(-baseZipCode.length) to check if it is contained at the end of the stirng
sure but u would be really unlucky to find your zip code elsewhere than where you put it
This worked perfectly because I am passing the zipcode as a request attribute. Thanks
@mlwacosmos Hey, once I changed my variable to a string, the zipcode started repeating again, for example I changed var zipcode = 48180 to var zipcode = '48180'. If you can, can you explain why your suggestion doesn't work anymore
@mlwacosmos I fixed it, I just put the value of the variable in this.href.indexOf() instead of the variable name

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.