View Full Version : How do I Get the Current Server URL & Path?

12-19-2005, 07:38 AM
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?

function create_form(id) {
var output = "<form action=\"....\".................;
document.getElementById(id).innerHTML = output;

Many thanks.

12-19-2005, 07:59 AM
You can embed php script inside javascript:

var output = '<form action="<? $_SERVER ?>" method="post">';
or use javascript:

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.