Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

No joy from document.referrer.title

Posted on 2007-07-24
22
Medium Priority
?
795 Views
Last Modified: 2008-01-09
I need to pass the title of a referring web page to a script to generate an e-mail. Document.referrer.title isn't doing it. Merely plugging document.referrer.title into the script where the reftitle variable shows below returned the string "undefined" in the subject of the e-mail. Creating a variable and defining it as document.referrer.title does the same. Short of leaving cookies, is there a simple expression that would pick up the title of a referring page? Or am I overlooking something in this script? (At least I'm getting the URL of the referrer properly, which is also important.)

Here's the latest version of the function:

function mailTest(list)
{
this.value = list;
url = document.referrer;
reftitle = document.referrer.title;
admin=list + "@domain.tld";
topic="I would like more information about";
{
parent.location.href='mailto:'+admin+'?subject='+topic+' '+reftitle+'&body='+url;
}
}

Script is invoked from a link <a href="javascript:mailTest('listname')"></a>

Thanks for your consideration.
0
Comment
Question by:kgm2002
  • 9
  • 9
  • 3
21 Comments
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 19557860
To get a title of current page, just need to use

document.title;

0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19557886
How is the new page opened?
Through regular links, in:
1. same window, or
2. new window
or
3. through javascript window.open
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 19557961
referrer Property gets the URL of the location that referred the user to the current page.

title is a property of document/page and not of URL
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:BraveBrain
ID: 19558060
Here's one way (that also works if you open the new document in the same window). It requires you to edit the links to the new page though:

Add an onclick handler to the links to the page:
<a href="linkedDocument.html" onclick="this.href+=((this.href.indexOf('?')!=-1)?'&amp;title=':'?title=')+escape(document.title)">linked document</a>

Then add a script to the head of the linked document (the one containing your code above):
<script type="text/javascript">
 <!--
  if (location.search) {
    var searchStr = location.search.substr(1);
    var searchArr = searchStr.split('&');
    for (var i in searchArr) {
      var searchVars = searchArr[i].split('=');
      eval('var '+searchVars[0]+'= "'+unescape(searchVars[1])+'";');
    }
  }
// your function follows
function mailTest(list)
{
this.value = list;
url = document.referrer;
reftitle = title; // set this to whatever you send it as in the location.search property
admin=list + "@domain.tld";
topic="I would like more information about";
{
parent.location.href='mailto:'+admin+'?subject='+topic+' '+reftitle+'&body='+url;
}
}

 //-->
</script>
0
 

Author Comment

by:kgm2002
ID: 19558080
Pravinasar: Not trying to get the title of the current page; trying to get the title of the referring page. I can get the URL of the referring page; that's not the problem. The referrer's title is what I want.

BraveBrain: Page is opened through a regular link; it produces a new message in the user's e-mail program of choice.

As the script is currently used, it generates an e-mail with the subject line "I would like more information about [page title]." It's designed to grab the current page title and insert it; I want to grab the referring page title and insert it into the square brackets, as well as inserting the page's URL into the body of the message. I can pick up the URL and insert it; I need to be able to insert the title. (For some reason, the overlords want an intermediate page "Help us improve our website!" between the contact link and the e-mail message. A useless layer of blather to me, but I don't write policy. But that's why I need the referrer's title as well as its URL.)
0
 

Author Comment

by:kgm2002
ID: 19558164
BraveBrain: Your suggestion produced a subject line with the title of the current page instead of the referrer, and a line in the body of the message of the current URL and the referrer's title. Well, we're getting closer; at least I have the referrer's title somewhere in the list. Now I just have to get the referrer's title into the subject line and the referrer's URL back into the body of the message. But I think we're getting there. Thanks.
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 19558192
You can not get title of document.referrer (as I posted earlier, as it is URL).

As pointed by BraveBrain, you need to send title of document via URL

0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19558194
That would be from your code ;)

In your code I replaced
reftitle = document.referrer.title;
with
reftitle = title;

Even better would probably be:
reftitle = title || "Unknown page title";
where you replace "Unknown page title" with whatever you want it to say if for some reason the title isn't retrieved properly.
0
 

Author Comment

by:kgm2002
ID: 19558204
Clarification: Page 1 has a link that says "Help us improve our web site!" Page 2 has a link that generates the desired e-mail (the scripting above). That second link needs to grab both the URL and the title of Page 1 and pass them along, the title in the subject and the URL in the body. As of now, the subject line has the title from Page 2 instead of Page 1, and the body of the message has "[URL from Page 2]?title=[title from Page 1]".
0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19558254
To make sure you don't collide with existing variables/properties:

Change to
<a href="linkedDocument.html" onclick="this.href+=((this.href.indexOf('?')!=-1)?'&amp;title=':'?reftitle=')+escape(document.title)+'&amp;refurl='+escape(location.href)">linked document</a>

Then, in your function:
refurl = refurl || document.referrer; // Note: renamed vars
reftitle = reftitle || "Unknown page title"; // set this to whatever you send it as in the location.search property

parent.location.href='mailto:'+admin+'?subject='+topic+' '+reftitle+'&body='+refurl;
0
 

Author Comment

by:kgm2002
ID: 19558390
BraveBrain: Nope: Still passing current title to subject line, "[Page 2 URL]?reftitle=[Page 1 title]" to message body. And I'm cutting and pasting your copy into mine, editing where necessary (filenames and the like). The object is to get "I would like more information about [Page 1 title]" in the subject line and "[Page 1 URL]" in the body of the message.

Now, I don't know if this is a complication or not; however:

The mail script is in a separate JS file so it can be included throughout the web site. In fact, there are about a dozen variations on the original in this file, mostly so I can change the admin variable when I need to swap domains. So every page on the site, including the referrer and the linkedDocument, incorporates the mail script file through a <script src=> tag. Every page reads in all the mail scripts and uses whichever one is referenced in the <a> tag.
0
 

Author Comment

by:kgm2002
ID: 19558488
OK, belay that. Idiot me posted an older version of the file over a newer one. No wonder things weren't working. [Duh.] BraveBrain, your solution actually does work; thank you so very much.
0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19558539
Can I see your current script?
It works fine here.

My links look like this:
<a href="linkedDocument.html?somevar=123" onclick="this.href+=((this.href.indexOf('?')!=-1)?'&amp;reftitle=':'?reftitle=')+escape(document.title)+'&amp;refurl='+escape(location.href); alert(this.href)">linked document 1 </a>
<a href="linkedDocument.html" onclick="this.href+=((this.href.indexOf('?')!=-1)?'&amp;reftitle=':'?reftitle=')+escape(document.title)+'&amp;refurl='+escape(location.href)">linked document 2</a>

And my linkedDocument.html like this:
<html>
<head>
<script type="text/javascript">
 <!--
  if (location.search) {
    var searchStr = location.search.substr(1);
    var searchArr = searchStr.split('&');
    for (var i in searchArr) {
      var searchVars = searchArr[i].split('=');
      eval('var '+searchVars[0]+'= "'+unescape(searchVars[1])+'";');
    }
  }
  var admin = 'listname@domain.tld';
  var topic = 'I would like more information about: ';
  function makeMail() {
    document.getElementById('mailto').value = admin;
    document.getElementById('subject').value = topic + reftitle;
    document.getElementById('mailbody').value = refurl;
  }
 //-->
</script>
</head>
<body onload="makeMail()">
To: <input type="text" id="mailto" /><br />
Subject: <input type="text" id="subject" /><br />
Body: <textarea id="mailbody"></textarea>
</body>
</html>
0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19558546
Oh. Nvm my last comment then. lol

Glad you got it to work. Thanks for points, grade and challenge :)
0
 

Author Comment

by:kgm2002
ID: 19804141
BraveBrain:

I hope you're out here. My apologies for the confusion. The originally accepted answer doesn't throw an Outlook message in MSIE, and I can't tell why. Your second solution would need some off-line discussion, inasmuch as it looks quite different, not only from the original solution, but also from the usual scripts we use on our web site; but I know the moderators wouldn't permit that, so can you join me here and explain how it works (or help me figure out why the original solution isn't working in MSIE)? Thank you.

kgm2002
0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19809829
Hi kgm2002,
Yeah, I'm still here.
Two question:
1. Which version of MSIE are you testing this in? It seems to work fine in my IE7.
2. What's the setting for Tools > Internet Options >> Programs > E-mail in the browser it doesn't work for? I've tried with both blank (since no default client was set up for email on this computer), Windows Live Mail (hotmail) and later Outlook Express. All three worked fine (blank will open the setup for Outlook Express and then process the email afterwards). Make sure you've got lowercase "subject=" and "body=" in the location string in the script (not Subject and/or Body).

The last script from my comments gathers the variables from location.search, as in the previously accepted solution, sets variables for admin (email to send to) and topic (the static part of the wanted subject). The rest (makeMail()) is only for debugging, where it inserts the to-address, the subject and the email body into form fields instead of actually sending the mail vars to the email client. This wa just because you earlier didn't get the wanted variables into the right places.
In the live script you would use
location.href='mailto:'+admin+'?subject='+topic+reftitle+'&amp;body='+refurl;

Any other variables (strings) you would want in the email's body would go in with refurl:
location.href='mailto:'+admin+'?subject='+topic+reftitle+'&amp;body='+varWithBeforeText+refurl+varWithAfterText;

0
 

Author Comment

by:kgm2002
ID: 19810290
1. Using MSIE 6.0. We're usually not on top of the latest version of anything here.
2. Program is listed as Microsoft Office Outlook, but IE6 won't throw an e-mail using the script I originally accepted.

Oh, and if it's important, I notice that the URL in the first document gets completely rendered in the address bar once the second file opens, i.e., http://some.location.tld/page2filename.html?reftitle=[page 1 title]&refurl=http://some.location.tld/page1filename.html
0
 

Author Comment

by:kgm2002
ID: 19885764
OK, I guess I'll have to put the working files out on the street for all to see. Go to http://aec.army.mil/dev/indexjean.html and click on the link that says "Help us improve our web site!" That will start the process. Once you get to the page called improve.html, it's the links near the top that allow you to report an error (one of which is actually so called) that refuse to start an Outlook message in MSIE 6.0. Ironically, and frustratingly, the links at the bottom of the page will throw an Outlook message, just showing a very strange URL in the message body.
0
 
LVL 11

Accepted Solution

by:
BraveBrain earned 1600 total points
ID: 19887813
Thanks. Now I found what's causing your problems.
Within script tags it's a good habit to use
//-->
instead of
-->

In your case it's the
-->
between the PopupWindow() function and the searchvars to vars converter that messes up IE (also in IE7).
Either remove it or comment it out and the page works fine.

Example:

<SCRIPT language=JavaScript>
<!--
function PopupWindow(page)
{
destination = page;      top.window.open(destination,'','toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=500,height=350');
}
//-->

<!--
  if (location.search) {
    var searchStr = location.search.substr(1);
    var searchArr = searchStr.split('&');
    for (var i in searchArr) {
      var searchVars = searchArr[i].split('=');
      eval('var '+searchVars[0]+'= "'+unescape(searchVars[1])+'";');
    }
  }
 
function settimer()
{
    setTimeout('location.href="response00.html"',3500);
}

//-->
</SCRIPT>
0
 
LVL 11

Expert Comment

by:BraveBrain
ID: 19887855
Note that the last --> that I changed isn't a needed change, but again, it's a good habit to always use that to avoid unnescessary bug testing. Appearantly IE doesn't like --> in the middle of scripts.

Also note that for CSS comments it's the opposite. --> is ok while //--> causes errors.
You've got some errors in your CSS by the way. If you open the page in Firefox and check the Error Console (Tools > Error Console in the Firefox menu) you'll get a list of rules to deal with ;)


0
 

Author Comment

by:kgm2002
ID: 19916650
So we do; thanks for the tip. Pity I can't pipe that to a text file or something where I can evaluate them later. I also notice the error console reads multiple copies of the same errors (does the same thing on the Experts Exchange site, FWIW, where there are a lot of problems with "Unknown property 'filter' [and a couple with "Unknown property 'display'", too], and they repeat numerous times).

However, NOW we have a solution. I've bumped up the points for putting you through it a second time. Thanks for the help. Again. My overlords will be happy. Now to figure out how to plug this into a JS file so it can be WORM populated throughout the site.

Oh, and thanks for the tip on the comments. We've been using the old comment form (without the slashes) from jump street; good to know that it causes problems in some uses. More piquedness to keep in check.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question