How to assign the value of the variable in JavaScript function into a Mason variable?

665 views Asked by At

I am getting window.location.href property on click of a button on client side in a javascript variable. My requirement is to send it back to server. How can I get the JavaScript variable value back in Mason code?

One option (which I have implemented currently) is to dynamically create a hidden text field with value set to window.location.href, and do a form submit.

How can use ajax here? I am looking for a ajax solution, how it is different from form.submit().

1

There are 1 answers

0
bvr On

The whole point of the problem is that javascript variable is available to js interpreter in the browser and not to the server side script. You need to somehow get the variable contents and send it back to server. This can be done either via form (which will at least reload the page) or XHR (main technology behind ajax).

I never did XHR directly, but using dojo framework it can look like:

dojo.xhrPost( {
    handleAs: "json",
    url:      "http://example.com/whatever",
    content:  { "data_url" : window.location.href }
});