Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I Get the Current Server URL & Path?

    Hi,
    I am writing a javascript function that insert html code into innerHTML. It's a form, and I need to assign the path of the server file into the <form action="....">. I don't want to hard code the path. How do I use javascript to do it for me? In php, I can user the $_SERVER variable, what about javascript?

    Code:
    function create_form(id) {
      var output = "<form action=\"....\".................;
      document.getElementById(id).innerHTML = output;
    }
    Many thanks.
    Last edited by dealmaker; 12-19-2005 at 07:47 AM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You can embed php script inside javascript:
    Code:
    var output = '<form action="<? $_SERVER ?>" method="post">';
    or use javascript:
    Code:
    var output = '<form action="' + location.pathname + '" method="post">';
    But if you don't specify the form action, the default action page is the current URL. So you can leave the action empty or not specify the action attribute at all.
    Last edited by glenngv; 12-19-2005 at 08:01 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •