Link to home
Start Free TrialLog in
Avatar of rivkamak
rivkamakFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of rivkamak

ASKER

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.
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

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.
Not relevant with Js
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:
https://www.experts-exchange.com/questions/28488776/jquery-changing-url.html?anchorAnswerId=40235179#a40235179
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial