0

I have an HTML form grabbing the previous page url and page title with javasscript but I need the page title to be the previous page too. It currently grab the page it is on. How do you grab the page title from the previous page?

Here is my code...

    <script type="text/javascript">// <![CDATA[
    function start() {
    var url = document.getElementById('url');
    url.value = document.referrer;
    var ptitle = document.getElementById('ptitle');
    ptitle.value = document.title;
    }
    onload = start;
    // ]]></script>
3
  • Pass the title in the request as a query param? May 17 '13 at 15:12
  • Do you have any example code I could try?
    – dnicin
    May 17 '13 at 15:14
  • That will depend on how you issue redirects.. May 17 '13 at 15:16
2

No you can't. You can get the referral URL (if it was passed by the browser), but you don't have any visibility of the content of the referring page.

If you're in control of the referring page, then change the link so that it passes the details you need in the URL query string.

If you don't have control of the referring page, you'll either have to ask them to do the above, or else I guess if you were desperate you could make an ajax call to the referring URL, reload the page content, and parse it to find out the info you need. (that doesn't guarantee that the content will be the same as it was when the user was there, though)

5
  • The problem is the user would be coming from any page. Not just one page from with in the site. The page could be coming from google or any search engine or from brother and sister sites within the organization. We just want to know exactly how they got to the form.
    – dnicin
    May 17 '13 at 16:04
  • In that case, the referral URL would seem more appropriate than the page title anyway. forget about the title, and just get the URL. Also note that this information is available to your server code too and it'll also be in your web server logs, so unless you're planning to actually show the user information about the page he's just come from, there's not much need to be doing any of this in Javascript.
    – Spudley
    May 17 '13 at 16:12
  • I already have the URL passing through the html form. The page title is a must. This will also get passed through the form and emailed to a non technical person along with all the information from the html form. They won't be viewing any server logs or anything like that. It just need to be grabbed and sent in the email.
    – dnicin
    May 17 '13 at 16:20
  • I got it! Thanks for all the help.
    – dnicin
    May 17 '13 at 17:19
  • 1
    The previous page url and previous page title are passed to the server in hidden input fields: <script type="text/javascript">// <![CDATA[ $( document ).ready( function() { var title = ''; var previousURL = document.referrer; $( "#url" ).val( previousURL ); $.ajax({ url: previousURL, success: function ( html ) { $( "#ptitle" ).val( $( html ).filter('title').text() ); } }); }); // ]]></script> <input id="ptitle" name="ptitle" type="hidden" />
    – dnicin
    May 17 '13 at 17:24

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.