tags:

views:

52

answers:

3

Hi There

I have a simple question how do I get a Request.Form within a script tag ?

<script language="javascript" type="text/javascript">
var sLandingPage = "";
var sInitContent = ""+Request.Form("textarea")+"";
var sInitDialogMsg = "";

any help would be appreciated

+1  A: 

You can't do request.form in javascript. But you can get things out of the query string if you post the form via get.

Avitus
+1  A: 

To read an url parameter from javascript you could use the following method:

<script type="text/javascript">
var sLandingPage = '';
var sInitContent = getUrlParam('textarea');
var sInitDialogMsg = '';

function getUrlParam(name)
{
    name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec(window.location.href);
    if(results == null)
        return '';
    else
        return results[1];
}
</script>
Darin Dimitrov
Works like a charm! Thanks Darin!!
Gerald Ferreira
+1  A: 

Javascript is running at client side, so if your want to get the text area's value before post your page to serve, use this :

var sInitContent = document.getElementById("textarea");

but if you want to get the posted value of your textarea, (I'm assuming you're using asp or asp.net) try this :

var sInitContent = '<%= Request.Form("textarea")%>';
Canavar
Thanks Canavar - This is actually the solution that I were looking for - I could not figure out why mine were not working cause I have used it a thousand times before - I were requesting a value that did not exists... Request.Form("textarea1") instead of textarea! Thanks
Gerald Ferreira
Text being injected into JavaScript must be suitably escaped, else you have a cross-site-scripting security hole.
bobince