+ Reply to Thread
Results 1 to 8 of 8

Thread: JS: referring page?

  1. #1
    dfmchfhf is offline x10Hosting Member dfmchfhf is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    3

    JS: referring page?

    Is there a (simple) way to get JavaScript to return the page that referred the code?

    I have some of code to add input to a MySQL database (through AJAX), and I want to make it use the same .js file to handle everything.

  2. #2
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: JS: referring page?

    you can use
    Code:
    location.href="http://site.you.want.to.go.com/";
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  3. #3
    djcustom is offline x10Hosting Member djcustom is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    15

    Re: JS: referring page?

    Try using

    Code:
    page = request.getHeader("referer");
    location.href = page;

  4. #4
    t2t2t's Avatar
    t2t2t is offline x10 Elder t2t2t is an unknown quantity at this point
    Join Date
    Sep 2006
    Location
    Europe, Estonia
    Posts
    690

    Re: JS: referring page?

    He didn't want to go back to the page the user got to that page from, he wants to enter the referring URL to MYSQL database for storing.

    Though it would be easier to be just done through php code, if you want to use javascript method, One way is to get the page before current one from (window.)history array.

    Btw djcustom, your script probably would fail on first line (tried request.getHeader("referer") in console):
    ReferenceError: request is not defined source=request.getHeader("referer");
    This post has been marked spam 52 times.


  5. #5
    dfmchfhf is offline x10Hosting Member dfmchfhf is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    3

    Re: JS: referring page?

    Right now, what I have is:
    PHP Code:
    <div id="messages"></div>
    <hr>
    <div id="form_part1"></div>
    <?php
    echo "<input type='hidden' name='ref' id='ref' value='" basename($_SERVER['PHP_SELF']) . "'>";
    ?>
    <div id="form_part2"></div>
    with the </form> tag in part2;
    Is it possible to collapse the entire form portion into JS?

  6. #6
    t2t2t's Avatar
    t2t2t is offline x10 Elder t2t2t is an unknown quantity at this point
    Join Date
    Sep 2006
    Location
    Europe, Estonia
    Posts
    690

    Re: JS: referring page?

    Can you tell more exactly what you want done, do you want JS to write the form?

    (Btw if you want to save from where did form get submitted from, just use $_SERVER['HTTP_REFERER'] in PHP when saving data)
    This post has been marked spam 52 times.


  7. #7
    dfmchfhf is offline x10Hosting Member dfmchfhf is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    3

    Re: JS: referring page?

    (part of) the JS file:
    Code:
    function subMsg(name, subj, cmt, ref)
    {
      //... (some code skipped)
      mTimer = setTimeout('udtCmt(\''+ref+'\');', 1);
    }
    
    function udtMsg(ref)
    {
      GetXmlHttpObject();
      if (xmlHttp!=null)
      {
        url="getMsgs.php?q="+ref+"&sid="+Math.random();
        xmlHttp.onreadystatechange=state_Change;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
      }
      clearInterval(mTimer);
    }
    
    function state_Change()
    {
      if (xmlHttp.readyState==4)
      {
        toDisp=xmlHttp.responseText;
        toDisp=toDisp+"\n<form name=\"messages\">\n<table><tr><td>Name:</td><td><input type=\"text\" name=\"name\" id=\"name\" autocomplete=\"off\" tabIndex=\"5\" accesskey=\"C\" maxlength=30 size=30 /></td></tr>\n<tr><td>Subject:</td><td><input type=\"text\" name=\"subj\" id=\"subj\" autocomplete=\"off\" tabIndex=\"6\" maxlength=100 size=70 /></td></tr>\n<tr><td>Messages:</td><td><textarea name=\"message\" id=\"message\" tabIndex=\"7\" rows=5 cols=50></textarea></td></tr></table>\n";
        document.getElementById('form_part1').innerHTML=toDisp;
        toDisp="<input type=\"button\" name=\"send_msg\" id=\"send_msg\" value=\"Submit\" tabIndex=\"8\" onClick=\"javascript:subCmt(document.getElementById(\'name\').value, document.getElementById(\'subj\').value, document.getElementById(\'message\').value, document.getElementById(\'ref\').value)\;\" />\n\n</form>";
        document.getElementById('form_part2').innerHTML=toDisp;
      }
    }
    (part of) getMsgs.php:
    PHP Code:
    echo "<table border='0' align='center' width='100%' style='table-layout:fixed;'>\n";
    echo 
    "<tr><td width='20px'></td><td></td></tr>\n";

    while(
    $row mysql_fetch_array($query_sel))
    {
      echo 
    "<tr><td colspan='2'><b>Subject: </b>" $row['pSubj'] . "</td></tr>";
      echo 
    "<tr><td colspan='2'><b>Posted </b>" $row['pDate'] . " <b>by</b> " $row['pName'] . "</td></tr>";
      echo 
    "<tr><td></td><td>" $row['pMsgs'] . "</td></tr>";
      echo 
    "<tr><td colspan='2'><hr></td></tr>\n";

    (part of) referring page:
    PHP Code:
    <p><div id="form_part1">JavaScript is not enabled on your computer.</div>
    <?php
      
    echo "<input type='hidden' name='ref' id='ref' value='" basename($_SERVER['PHP_SELF']) . "'>"
    ?>
    <div id="form_part2"></div></p>

  8. #8
    nightscream is offline x10 Lieutenant nightscream is an unknown quantity at this point
    Join Date
    Feb 2006
    Location
    Hallaar, Belgium
    Posts
    474

    Re: JS: referring page?

    I'm not sure what you mean but you can refer to a page in js with window.location = "URL"
    ------------------------------------------------------------------------------------------
    If you have any troubles with a website or a script, just send me a pm.

    I also code websites in xHTML/css, can code javascript and php too if needed

+ Reply to Thread

Similar Threads

  1. [REQ][$$$]1 page image flash redesign for choclate sales
    By tgkprog in forum The Marketplace
    Replies: 5
    Last Post: 11-17-2008, 09:53 AM
  2. Use PHP to add Content to page through Admin
    By goldy30 in forum Programming Help
    Replies: 1
    Last Post: 11-12-2008, 11:13 PM
  3. Unique php page
    By bunglebrown in forum Programming Help
    Replies: 48
    Last Post: 11-08-2008, 08:59 AM
  4. Replies: 0
    Last Post: 10-19-2008, 09:40 PM
  5. Passing variables from page to page
    By os242 in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 09-15-2007, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers