Reputation: 101
following is a function of a .js file of my website which is included in the head section of a webpage,test.html
function test_redirect() {
var urlCurrent = document.URL;
window.location = "http://webanywhere.cs.washington.edu/beta/?starting_url=" + urlCurrent;
}
$.test_redirect();
As it is clearly visible from the javascript code that I am trying to redirect the user who opens the page test.html to another page which accepts the URL of the correct page I am on as a parameter in the query string and performs some operations on it.
The problem arises when the redirected web page loads multiple times. The redirected page actually opens the same test.html with an injected script page, but the redirect starts again.
Can someone please help me to figure out where the problem lies? .
Upvotes: 0
Views: 770
Reputation: 6552
Check the referrer:
function test_redirect() {
if( document.referrer.indexOf('http://webanywhere.cs.washington.edu') < 0 ){
var urlCurrent = window.location.url;
window.location = "http://webanywhere.cs.washington.edu/beta/?starting_url=" + urlCurrent;
}
}
$.test_redirect();
Upvotes: 1
Reputation: 707326
There are various ways to decide NOT to do the redirect on subsequent loads of the page that come from the third party:
Right before you redirect, set a cookie with a fairly short expiration (e.g. 1-2 minutes). If the cookie exists upon load, don't redirect because you've just done a redirect a short time ago so this page load must be from the third part. If the cookie doesn't exist, then do the redirect.
Have the third party website load your page the second time with some sort of query parameter that you can identify as being put there by the third party website so you know not to redirect. For example, what happens if you add "?noredirect=true" (properly encoded) onto the end of the URL that you redirect to. Will the third party site leave the "?noredirect=true" there on your URL when it comes back to you can detect that and avoid the redirect?
Look at the document.referrer and see if it's the third party page. Some user's may configure their browsers to block referrers so this is not 100% reliable.
Have your page opened the first time (from whatever link it came from) specify a query parameter that specifies that the redirect should be allowed. This is only feasible if your page is only linked to from places you control and won't likely be bookmarked.
Upvotes: 1