0

I want to check whether a string returned by document.referrer contains desired string or not. For example: I have used document.referrer as below and used split.

var ref1= document.referrer.split('/');

But my problem is to check whether the returned string from document.referrer contains a particular string or not. eg:

ref1 contains home/index.html or not.

Anyone please help me.

1 Answer 1

2
//var ref1= document.referrer;
var ref1= "http://yoursite.com/home/index.html?pp=Nav_shop&sId=340076986";

var search = "home/index.html";
if (ref1.indexOf(search) > -1)
    alert('Found');
else 
    alert('Not Found !');

JSFIDDLE DEMO

4
  • Does this work if there is long string after index.html? Like there may be string like yoursite.com/home/index.html?pp=Nav_shop&sId=340076986 Sep 7, 2014 at 8:30
  • you mean long query strings? provide example to clarify your question
    – Farshad
    Sep 7, 2014 at 8:32
  • Yes long query string. See example above Sep 7, 2014 at 8:36
  • Yes of course, I have updated the ref1 string , you can test in jsfiddle link with any url you want. above code seeking for whatever you put in search variable for example "home/index.html" in ref1 string.
    – Farshad
    Sep 7, 2014 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.