0

The problem I face at this moment is the following. I have 2 links, link A goes to the frontpage/home page of a store and link B which goes to a specific page within the store e.g. a product category. In order for link B to pull the page properly(blank page), it requires link A to have been visited. My guess is that link A sets cookies/session of said person that then allows travel throughout the store.

The solution I have in mind is when user presses link B, call link A as if they had pressed it originally without physically redirecting to it to then redirect to link B.

I have read a few other questions regarding this yet they haven't really answered my predicament. The closest to it was: redirect without to leave the current page

I am developing a website in html/php. I have not used the terms/functions mentioned in the above question. I did read in another question, can't find it, about cURL however I am running php version 5.4.3(a poster mentioned cURL was included from php 5.4.3). Does that mean cURL is already installed or do I have to install it anyway?

EDIT: The reason I ask the above is because the store is run and managed not by me. The scenario being I am an independent distributor with the store being held and run by the distributor.

Community
  • 1
  • 1
madman
  • 21
  • 2
  • 7
  • Running cURL from your server won't set cookies in the end-user's browser. – jfriend00 Apr 26 '14 at 17:16
  • You could of course set the cookie yourself. Either way its not really clear _what_ you are able to do. Do you have control over the webserver? Do you have control over the link? If so, in what way? Is JavaScript an option? I think you need to give us more information. – alex Apr 26 '14 at 17:21
  • If you have not read the edit I made, please do. If you have and that's has still not answered your question, I have no control over the server the store is held in nor the link other than using it as a link. I am willing to try what works however I prefer the simplest solutions over intricate. – madman Apr 26 '14 at 17:29
  • 2
    I think you should contact the distributor and explain what and why you're trying to do and see if there's any way that their store implementation supports it. Linking directly to the buy link for a specific product WILL increase the likelihood of a purchase so the distributor SHOULD want to help you solve this issue. – jfriend00 Apr 26 '14 at 17:35

1 Answers1

0

Your best hope is to create an iframe in your own page (it doesn't have to be very big and probably doesn't even have to be visible) and load link "A" into the iframe. When that iframe has completed loaded, you can then take the user to link "B". Assuming that site "A" doesn't prevent itself from loading into an iframe, that should serve to establish any site initialization code (like a cookie).

Because both A and B are a different domain than your own page, you are restricted from doing anything directly to those pages.

You can dynamically create an iframe and monitor when it is loaded like this:

function preloadSiteThenRedirect(linkA, linkB) {
    var iframe = document.createElement("iframe");
    iframe.onload = function() {
        window.location = linkB;
    };
    iframe.src = linkA;
    // position iframe off screen to the left
    iframe.style.position = "absolute";
    iframe.style.left = "-1000px";
    iframe.style.width = "800px";
    iframe.style.height = "800px";
    document.body.appendChild(iframe);
}

FYI, you could also cookie your own page once you've done this once so that you know you probably don't have to do it again within some cookie expiration time period (you could investigate the cookie expiration period on site A).

FYI, you will need to make sure that site A allows itself to be framed. Some sites do not. For sites that do not prevent being framed, the above logic seems to work.

Here's a working demo: http://jsfiddle.net/jfriend00/TXTx9/


For your link to site B in your page, you can do this (I'm assuming you have jQuery from some other comments you made):

HTML:

 <a class="directLink" href="link B">Go directly to Product B</a>

Javascript:

 $(".directLink").click(function(e) {
      // prevent action of the link
      e.preventDefault();
      // call the preload, then redirect function
      preloadSiteThenRedirect("URL to main page", this.href);
 });

FYI, you can have as many links in your page as you want with the class="directLink" and this code will manage all of them.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • You beat me by a few seconds, I was just looking up the functions needed for doing exactly this with jQuery. – 11684 Apr 26 '14 at 17:38
  • @11684 - sorry, but I don't want to propose a jQuery solution when there was no indication of jQuery in the OP's question. – jfriend00 Apr 26 '14 at 17:45
  • The OP said he was using Bootstrap in a comment on my answer (which I deleted, don't know if you can still see it). Here is the comment: `I assume jQuery is available given I am using Bootstrap. [...]`, and bootstrap includes jQuery. – 11684 Apr 26 '14 at 17:47
  • @11684 - I didn't notice that bit about Bootstrap. I appreciate the extra effort you went to, but I already put a plain JS version in my answer already. – jfriend00 Apr 26 '14 at 17:50
  • Why not just `iframe.style.display = "none"`? I assume you didn't do that for a reason, but why wouldn't it work? – 11684 Apr 26 '14 at 17:50
  • @11684 - I wasn't entirely sure that everything in the iframe would render normally and be 100% the same as the user visiting it in the browser if it was `display: none` so I thought this was slightly safer. The OP could test `display: none` and see if it worked. – jfriend00 Apr 26 '14 at 17:52
  • As far as checking whether the iframe has loaded, I have found another question relating to that issue but I am not entirely sure how that actually checks whether an Iframe has loaded or not. Question: http://stackoverflow.com/questions/12267010/how-can-i-detect-whether-an-iframe-is-loaded – madman Apr 26 '14 at 17:57
  • @jfriend00 - Also, I guess I would be placing the above function or call the above function in the link of link B e.g. the ` function `? EDIT: actually, no that doesn't look right at second glance. Not entirely sure where it would go other than inside the `` tag? – madman Apr 26 '14 at 18:08
  • @madman - you would create an event handler for your link and run this javascript instead of letting it follow a link. FYI, I've updated my code with the `.onload` monitor and added a working demo. – jfriend00 Apr 26 '14 at 18:12
  • @madman - I've added to my answer how you can take over your links and hook this code into it. – jfriend00 Apr 26 '14 at 18:18
  • @jfriend00 - So to be clear, I could have the function saved into a single .js file which is loaded as a script at the start(or end as I hear the page loads quicker with it at the end?). Add the relevantly named id to each button/a href (empty e.g. `href=""` or does it have to be a button?) link of link B so whenever it is clicked on it runs the .js rather than follow the link. Thanks for the help so far, I will look to contact the distributor to see what they say however wanted to have this running incase they don't have an answer. – madman Apr 26 '14 at 18:24
  • @madman - sorry, but I don't understand your latest comment at all. I've added a link to a fully working demo to my answer and put the code in my answer too. You should be able to answer your questions by looking at that. It can work with an href if you use `e.preventDefault()` as I've shown or you could use a button to - any way you want to launch an event handler based on a click will work. – jfriend00 Apr 26 '14 at 18:37
  • @jfriend00 - Ok, I have attempted to use your answer. At the moment I am getting no response from the link I have used your answer on, I tested link A and B to see if it will show in an iframe and it does. I have the function in a php file which is included at the start, the javascript in a seperate .js file called after jquery is called and made the necessary changes to my html with one modification to your answer being having directLink as an id rather than class. For this I did change .directLink to #directLink in the javascript code. – madman Apr 26 '14 at 19:10
  • The concept works so I'd have to see your specific implementation to know what isn't right. – jfriend00 Apr 26 '14 at 21:32
  • @jfriend00 - Got it working, worked out from an error on another page that the function is not a php function but a js function. Moving that over to the file containing the js code got it working. Accepted this answer, again thanks for the help which includes you too 11684. – madman Apr 26 '14 at 22:24