-3

I need to get URLs from my referring page via jQuery. The URLs are inside an <a> element that looks like this in HTML:

<a href="http://localhost/sr19repairables/vehicles/repairables/2011-chrysler-town-country-touring-minivan-4d/" title="2011 Chrysler Town &amp; Country Touring Minivan 4D">

There are multiple <a> elements on the referring page and I need to get the URLs from all of them and put them in an array. Any help?

I have a button on my second page with this jQuery attached to it:

$(document).ready(function(){
    $(".single_vehicle_previous_button").click(function(){
        var referrer = document.referrer;
            if (document.referrer == "http://localhost/sr19repairables/vehicles/rebuilt-vehicles") {
                alert(value=referrer);
            };
            if (document.referrer == "http://localhost/sr19repairables/vehicles/repairables/") {
                alert(value=referrer);
            };
            if (document.referrer == "http://localhost/sr19repairables/vehicles/") {
                alert(value=referrer);
            };
    });
});

So if my referring page is one of these three, I want it to get the URLs in the anchors on those pages. The alert is just for testing purposes. Does that help clear things up?

claronic
  • 35
  • 10
  • 1
    I think you need to rethink your requirement. What if your page is opened from "google.com" - what use would getting all the URLs be? Better to pass the information you need from the previous page (assuming it's under your control). – freedomn-m Oct 23 '19 at 14:20
  • @freedomn-m I am going to check if the referring page is one of only three URLs and if it isn't, then perform another action. And how would I pass this information along from the previous page (it is under my control) – claronic Oct 23 '19 at 14:24
  • 1
    Well, that's *completely* different from what you've asked. Q: get *all* URLS from all anchors on previous page. Comment: see if document.referrer matches – freedomn-m Oct 23 '19 at 14:26
  • 1
    Keep in mind, that some sites prevent sending HTTP Referer Headers. https://stackoverflow.com/questions/6817595/remove-http-referer – Rumplin Oct 23 '19 at 14:41
  • @freedomn-m The reason I didn't ask about checking the referrer is because I knew. Check my edit – claronic Oct 23 '19 at 14:53
  • Ok, that does make more sense. – freedomn-m Oct 23 '19 at 14:57

1 Answers1

0

If you already know it's on the same site (as you've checked the url), then you shouldn't have the usual problems with loading random pages via ajax.

You can use $.ajax to get the page, then jquery to parse the html returned:

$.ajax(document.referrer)
    .done(function(html) { 
        var urls = $("<div/>").html(html)
                     .find("a")
                     .map(function(i, e) { return $(this).attr("href"); }
    });
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • It's returning `[object Object]` Any ideas? My anchor is inside a `
  • ` and my `
  • ` is inside a `
      ` and finally, my `
        ` is inside my `
        `
  • – claronic Oct 23 '19 at 15:40
  • Don't use `alert()` to view any output - it's always `[object Object]` - use `console.log(urls)` - it's an array of urls as requested. – freedomn-m Oct 23 '19 at 16:16
  • Is there any way to view the array of URLs? My next step is to find the URL of my current page in that array and get the URL that is before it. – claronic Oct 23 '19 at 16:51
  • Well, you asked specifically for an array of URLs ("*I need to get the URLs from all of them and put them in an array*"), so one would assume you would know what to do with it. Add a `debugger;` statement and use the browser debugging tools. Or use `console.log(urls)` as suggested above. If you need to do something else with them, then ask a new question. – freedomn-m Oct 23 '19 at 16:53
  • Note: there was a typo in the answer - it should be `.attr("href")` – freedomn-m Oct 23 '19 at 16:54
  • Ok, I wondered, because I added the console.log(urls) as suggested and didn’t recieve any array of URLs – claronic Oct 23 '19 at 16:59
  • Might have been due to the typo - try again using `.attr("href")` - if that still doesn't work, then what do you get with the console.log(urls)? – freedomn-m Oct 23 '19 at 17:02
  • Object { 0: "http://localhost/sr19repairables", 1: "http://localhost/sr19repairables/", 2: "http://localhost/sr19repairables/vehicles/", 3: "http://localhost/sr19repairables/vehicles/rebuilt-vehicles/", 4: "http://localhost/sr19repairables/vehicles/repairables/", 5: "http://localhost/sr19repairables/vehicles/request-a-vehicle/", 6: "http://localhost/sr19repairables/contact-us/", 7: "http://localhost/sr19repairables/location/", 8: "http://localhost/sr19repairables/hours/", 9: "http://localhost/sr19repairables/", … } It's returning this and I can expand it and get everything in the page. – claronic Oct 23 '19 at 17:54
  • It also has the following warning: Ignoring get or set of property that has [LenientThis] because the “this” object is incorrect. 2011-chrysler-town-country-touring-minivan-4d – claronic Oct 23 '19 at 18:00