Category Archives: JavaScript

JavaScript

How to encode URI components in javascript?

JavaScript provides an encodeURIComponent() function to encode uri components. This function encrypt the special characters provided in the url such as “space , / ? : @ & = + $ #“.
Syntax
encodeURIComponent(uri)
Example

var uri="codesolution.org/calculate-time-between-two-dates/";
var encodeduri=encodeURIComponent(uri);
$("#uri").html(uri);
$("#encodeduri").html(encodeduri);


Html

<h2>
How to encode URI components in javascript?
</h2>
<b>URI:</b> <div id=uri></div>
<b>Encoded URI:</b> <div id="encodeduri"></div>


Result
How to encode URI components in javascript?
URI:
codesolution.org/calculate-time-between-two-dates/
Encoded URI:
codesolution.org%2Fcalculate-time-between-two-dates%2F
Try it

How to Pass Value across Application or Post to url

To post value across Application or Post to url.
Write the below code on the Source application.

<script>
        function post_to_url(path, params, method) {
            method = "post"; // Set method to post by default, if not specified.            
            var form = document.createElement("form");
            form.setAttribute("method", method);
            form.setAttribute("action", path);
            form.setAttribute("target", "_blank");
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "ValueName");
            hiddenField.setAttribute("value", params);
            form.appendChild(hiddenField);
            document.body.appendChild(form);
            form.submit();
        }
</script>

Event on which you want to post the value. Write the below code on the event.

ClientScript.RegisterStartupScript(this.GetType(), "hwa", "post_to_url('http://Page Url','" + Value to Pass + "','post');", true);

How to get value on other Application.
Write the below code on Destination Application

String Value = Request.Form["ValueName"];