jquery changing url

Posted on 2014-08-01
Medium Priority
Last Modified: 2014-08-04
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?
Question by:rivkamak
  • 4
  • 4
  • 2
LVL 58

Accepted Solution

Gary earned 1000 total points
ID: 40235063
Code is fine - do you have a link to the page in question?

LVL 111

Expert Comment

by:Ray Paseur
ID: 40235179
This worked for me: http://iconoun.com/demo/temp_rivkamak.html

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<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>
    $("a.offlink").attr("href", "http://www.kars4kids.org/ppc/long-form.asp");

<title>HTML5 Page with jQuery AJAX Loader</title>
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>

Open in new window

HTH, ~Ray

Author Comment

ID: 40238216
My page is here:
 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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 58

Expert Comment

ID: 40238220
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

LVL 111

Expert Comment

by:Ray Paseur
ID: 40238898
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.
LVL 58

Expert Comment

ID: 40238933
Not relevant with Js
LVL 111

Expert Comment

by:Ray Paseur
ID: 40238972
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:

Author Comment

ID: 40239070
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?
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 40239127
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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question