3

I am working on a mobile application on iOS 8. document.referrer seems to always be empty. I have tried to check if it exists by using:

if( 'referrer' in document ) {
    alert('referrer ' + document.referrer);
}

and it throws an alert, but the value is always empty, whether I am accessing it from another page or if I am accessing the page for the first time. I have also included the meta tag:

<meta name="referrer" content="always">

How do I get the value of document.referrer?

EDIT: I also forgot to mention, that I am not working on a native iOS app. I am using cordova and HTML5 and JS.

user81371
  • 91
  • 1
  • 10
  • How are you accessing your page? The referrer is always empty if you access the page directly, i.e., not through a link. – klaussner Jan 02 '15 at 12:42
  • I have read up enough to know that it's going to be empty, when you access the link directly for the first time. Any other time, the redirect happens through a location.href="something.html". When I am in something.html, document.referrer is still empty. – user81371 Jan 03 '15 at 05:48

1 Answers1

2

Since we don't know anything about your native wrapper, it's possible there are a couple of things going on:

  1. document.referrer is derived from the HTTP header Referer (note historical misspelling). It's possible that something in your iOS code is setting or suppressing that HTTP header.

  2. If your web app is hosted using URLs with file schema (as in file:///foo.html), browser engines will not set the HTTP Referer header to prevent leakage of sensitive information. (You wouldn't want to send local filenames to a remote webserver if, for example, you had a local webpage that linked out to a remote site.)

If you find that one of these is happening, you can manually set the Referer header as described in "Specifying HTTP referer in embedded UIWebView"

Community
  • 1
  • 1
Palpatim
  • 9,074
  • 35
  • 43
  • Hi, thanks for your answer. I don't use native iOS, so the link that you referred me to wouldn't work I think? – user81371 Jan 03 '15 at 05:51
  • Then you'll need to provide more detail about your application--how are you hosting/serving/launching it? What about it makes it an "iOS" web app? How exactly are you navigating to a page on which you're looking for the referrer? – Palpatim Jan 05 '15 at 15:18
  • Sorry about that. I am using cordova, HTML5 and JS. I am using location.href="page-path" to navigate to the page. Does this information help? – user81371 Jan 08 '15 at 07:10
  • You still haven't answered how your pages are hosted. How exactly is your web app accessing pages? Are they hosted from a server? Bundled with your application? If the latter, you're not going to get a `Referer` header, as described above. – Palpatim Jan 08 '15 at 15:12
  • I am not sure, actually. I don't think a server is being used to host the pages - the pages are being bundled with the application. In that case, the referrer headder won't be set? What is strange is that, we didn't have this problem till iOS 7.1, the problem has only surfaced with iOS 8. I am not sure how to use the link you've provided since I have no experience with native iOS development. Thank you so much for your patience. – user81371 Jan 10 '15 at 04:34
  • Hi, I can confirm that we're accessing local file paths using file:// If I had to set the document.referrer variable manually but I am working on a cordova app, and therefore confined to using HTML and JS (and not objective C) for the most part, what do I do? – user81371 Jan 19 '15 at 05:45
  • Caveat: I don't develop with Cordova, but my read on this is: if you can't use Objective-C at all, I don't see how you're going to accomplish this. You may be able to find an existing Cordova plugin that accomplishes what you're after, but I didn't find any with a cursory Google search. – Palpatim Jan 19 '15 at 14:24