jquery changing url

I am trying to set up that every time a specific url shows up on my page it should change to a different url
something seems to be wrong with my code because it's not working.
   $("a[href='http://www.kars4kids.org/motorcycle-donation.asp']").attr('href', '//www.kars4kids.org/ppc/long-form.asp');

Open in new window

The page isn't throwing an error.
What am I doing wrong?
rivkamakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Code is fine - do you have a link to the page in question?

http://jsfiddle.net/GaryC123/g5Y9H/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
This worked for me: http://iconoun.com/demo/temp_rivkamak.html

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    $("a.offlink").attr("href", "http://www.kars4kids.org/ppc/long-form.asp");
});
</script>

<title>HTML5 Page with jQuery AJAX Loader</title>
</head>
<body>
Use View Source, then
<a target="_blank" class="offlink" href="http://www.kars4kids.org/motorcycle-donation.asp">Click to go to http://www.kars4kids.org/ppc/long-form.foo</a>
</body>
</html>

Open in new window

HTH, ~Ray
0
rivkamakAuthor Commented:
My page is here:
http://www.kars4kids.org/ppc/long-form.asp
 I set a cookie first thing on the page, and then lower down, I check if there is a cookie to change that link.
the console. logs 'got here' right before i run that line of code, but it doesn't work for me.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GaryCommented:
That's because it's firing before the page is loaded.
Wrap that function in a document ready function so it only executes after the page has finished loading i.e.

$(function() {
   if ( getCookie("indexname") == "ppc" ) {
	   console.log('got here');
   $("a[href='http://www.kars4kids.org/motorcycle-donation.asp']").attr('href', '//www.kars4kids.org/ppc/long-form.asp');//   $("a").attr("href", "http://www.kars4kids.org/motorcycle-donation.asp").css('background-color', '#F00');
 }
})

Open in new window

0
Ray PaseurCommented:
I set a cookie first thing on the page, and then lower down, I check if there is a cookie...
Cookies are sent from the client to the server with each HTTP request.  This means that the cookie you set in response to the first request will not be sent back to the server until the client makes a second request.  If you want to have some stateful information that is all part of the same script, you might want to have some other variable besides a cookie.
0
GaryCommented:
Not relevant with Js
0
Ray PaseurCommented:
Not sure about the logic related to the cookie (TL;DR), but document.ready() is highly relevant.  Please see line 10 in the example here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_28488776.html#a40235179
0
rivkamakAuthor Commented:
forget about the cookie part for the moment. it doesn't matter, and that's not the question.
 I was just trying to explain that the code was reaching that point.
but it's not working anyway.
why not?
0
Ray PaseurCommented:
I think this man page explains it pretty well.  JavaScript in "open code" (ie: not inside a function) gets run immediately, as soon as the browser encounters it.  Other page elements may not have loaded, but that doesn't stop the process.  In order to wait until the page elements are loaded, we use document.ready().  The ready() moment is usually pretty fast; images are not needed for the document to be ready.  The effect of using document.ready() as illustrated above is to wrap the jQuery statements in a function.  JavaScript functions are interpreted at page load time, but they are not run until an event triggers them.  This is different from JavaScript instructions in open code.  In the case of the example I posted above, the event that triggered the jQuery change of the href was the document.ready() event.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.