window.location.href form

Discussion in 'JavaScript' started by Jboo, Nov 11, 2007.

  1. #1
    Hi,

    I'm trying to add a form on my website so that the url and page title can be copied (basically so that anyone can copy a link to a specific page). So what I need to do is create the form somewhere on the page with a href and title statement but for javascript to fill in the url and title fields.

    Can anyone point out how to do this?

    Thanks for any help.
     
    Jboo, Nov 11, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Your JavaScript should be something like this
    <script type="text/javascript">
    function init()
    {
        document.getElementById("url").value = window.location.href;
        document.getElementById("title").value = document.title;
    }
    window.onload=init;
    </script>
    HTML:
    And the HTML form
    <form>
    URL: <input type="text" value="" id="url" readonly="readonly" />
    Title: <input type="text" value="" id="title" readonly="readonly" />
    </form>
    HTML:
     
    phper, Nov 11, 2007 IP
  3. Jboo

    Jboo Active Member

    Messages:
    229
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks for the help. Is there anyway it can altered slightly so that the form is formatted for the link code already, like this:

    <form>
    URL: <a href="<input type="text" value="" id="url" readonly="readonly" />"><input type="text" value="" id="title" readonly="readonly" /></a>
    </form>
    Code (markup):
    Thanks again for your help, it's much appreciated.
     
    Jboo, Nov 12, 2007 IP
  4. JeffHood

    JeffHood Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This will set the links anchor text to the title of the page while referencing the link to the current page:

    <script type="text/javascript">
    function init()
    {
        document.getElementById("url").href = window.location.href;
        document.getElementById("title").innerHTML = document.title;
    }
    window.onload=init;
    </script>
    HTML:
    <a href="" id="url"><span id="title"></span></a>
    HTML:
     
    JeffHood, Nov 12, 2007 IP
  5. Jboo

    Jboo Active Member

    Messages:
    229
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Sorry for the late reply, but thanks for your help, it's much appreciated.
     
    Jboo, Nov 19, 2007 IP