rivkamak
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.
What am I doing wrong?
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');
The page isn't throwing an error.What am I doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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');
}
})
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
https://www.experts-exchange.com/questions/28488776/jquery-changing-url.html?anchorAnswerId=40235179#a40235179
ASKER
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?
I was just trying to explain that the code was reaching that point.
but it's not working anyway.
why not?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
HTH, ~Ray