Sending JavaScript object to other page - javascript

When I click on button on my webpage it should redirect me to other page with JavaScript object. I am not able to code for sending java script object with onclick event to other page. Help me in code for sender side page and receiver side page.
Java script object contains array of multiple key value pairs, and I want to send this array to other page.
Example:
data= { {region: "Maharashtra",sector: "Manufacturing",size: "MSME",year: "2008"} and many }
I apologize if my question is not clear.

There are some ways to do this on JS:
Using Cookies
Using QueryString
Using LocalStorage
Note :
Action using above technique only affected on client side only. Except, targeted page sending it into server.

you don't need java script, enough add params to your <a src=""></a>
for example in html:
<a src="www.something.com/something?region=Maharasta&size=MSME"> Link </a>
or you can add params to your url dynamic for example by jQuery:
var params = { fuu:1680, buu:1050 };
var str = '?' + jQuery.param( params );
$( "#myLink" ).attr('src', $('#myLink").attr('src') + str)

Related

passing js value to another php page using java script and receiving value

I want to to pass a js variable to another page ..what to do..code is given bellow..
Code
`
$('#disInfo').on('click','tr',function(){
var obj = $(this);
var result= obj.find('td').eq(1).html();
window.location.href='http://localhost/Trying/search_cus.php';
});
`
I want to send the value of result variable with page link and how to receive the value in another page
You could pass it via an url parameter. If result contains html or characters that cannot be used in an url then you need to encode it first.
$('#disInfo').on('click','tr',function(){
var obj = $(this);
var result = encodeURIComponent( obj.find('td').eq(1).html() );
window.location.href='http://localhost/Trying/search_cus.php?result=' + result;
});
On the target page you can get url parameters via window.location.search ... there's plenty of examples on stackoverflow on how to do that.
NOTE: be aware that the server will also get the request for ?result=whatever. If the server side code processes this (eg PHP: $_GET['result']) you must ensure it sanitizes the value to prevent malicious code injection.
BTW, other ways of passing data is via cookies, sessionStorage, localStorage, etc.

How to set value in .net tag helper attribute using javascript?

I am trying to set parameter value read from a textbox on the page to this link:
<a asp-action="RetrieveEmailRecipients" asp-route-clientnumber="getClientNumber()">Retrieve Email Recipients</a>
And I have Javascript down somewhere else at the bottom of the page. But appreatly this is not the right way to pass value to MVC action method's parameter.
What parameter ClientNumber read in action is whatever the string/value in asp-route-clientnumber, in this case the value is getClientNumber().
What is the correct way to set value in the attribute using javascript/jquery?
you are mixing server side code and client side code together
I am assuming your getClientNumber() is a javascript function
Tag Helper is generated before javascript comes in, so when server is generating the html, it has no knowledge of what getClientNumber()
is so will treated as literal string
So the proper way of doing it will be modify the url with jquery when client number available
Something like:
give the anchor an identifier - so you can access it using jquery easily and remove the route
<a id="aRetriveEmail" asp-action="RetrieveEmailRecipients">Retrieve Email Recipients</a>
modify the attribute of the anchor to include your clientNumber whenever you have clientNumber ready, or use javascript to navigate to it
$(function(){
$("#aRetriveEmail").click(function(){
// example of how modifying href when client number ready
// $(this).attr("href", $(this).attr("href") + "?clientnumber=" + getClientNumber());
// or navigate to the url
location.href = $(this).attr("href") + "?clientnumber=" + getClientNumber();
})
})

Sending and getting data via $.load jquery

I'm writing a system in HTML5 and Javascript, using a webservice to get data in database.
I have just one page, the index.html, the other pages i load in a <div>
The thing is, i have one page to edit and add new users.
When a load this page for add new user, i do this:
$("#box-content").load("views/motorista_add.html");
But, i want send a parameter or something else, to tell to 'motorista_add.html' load data from webservice to edit an user. I've tried this:
$("#box-content").load("views/motorista_add.html?id=1");
And i try to get using this:
function getUrlVar(key) {
var re = new RegExp('(?:\\?|&)' + key + '=(.*?)(?=&|$)', 'gi');
var r = [], m;
while ((m = re.exec(document.location.search)) != null)
r.push(m[1]);
return r;
}
But don't work.
Have i an way to do this without use PHP?
This won't work. Suppose your are loading the motorista_add.html in a page index.html. Then the JS code, the function getUrlVar(), will execute on the page index.html. So document.location that the function will get won't be motorista_add.html but index.html.
So Yes. To do the stuff you are intending, you need server side language, like PHP. Now, on the server side, you get the id parameter via GET variable and use it to build up your motorista_add.php.
You can pass data this way:
$("#box-content").load("views/motorista_add.html", { id : 1 });
Note: The POST method is used if data is provided as an object (like the example above); otherwise, GET is assumed. More info: https://api.jquery.com/load/

How to pass value to another static page via javascript?

I am trying to pass value to another page and read the value in the another page. I use only JavaScript and jQuery. Not use others language as PHP. I don't want to be as like (http://www.example.com/?value=test). I am trying to use POST or GET method.
On first page:
$.post("second_page.html",{q:"test"});
window.location.replace("second_page.html");
On second page:
var location_query = window.location.search.substring(1);
var vars = location_query.split("&");
for (var i in vars) {
var param = vars[i].split("=");
params[param[0].toString()] = param[1];
}
If I pass value as like http://www.example.com/?q=test , the second page script is read the value, but I use this line: $.post("second.html",{q:"test"}); not read.
How to pass value from first page to another static page via JavaScript or jQuery or ajax?
See this post says:
POST data is data that is handled server side. And Javascript is on client side. So there is no way you can read a post data using JavaScript.
but you can use cookies to read and store values:
<script src="/path/to/jquery.cookie.js"></script>
you can get it from here
Try with reading the documentation and see if this works for you.
Without using GET or POST you can transfer the content using HTML5 Local Storage or
Web SQL
$.post and $.get send AJAX request and waiting response on current page, you cant send params for next window.location.replace by this.
Use url get-params window.location.replace("404.html?q=404"); and look this for get params on second page.

How to remove params from URL after form is submitted?

When I use the following link in my browser, a record is populated in the form on this page to edit.
http://vcred.dev/#personal/myacademicdetail?record_id=15
When I click 'SAVE' button, record is successfully updated in database and form with updated values is still in front of me. It is OK for me. But I have only one problem after form is posted. In Address bar URL is still same as before post.
http://vcred.dev/#personal/myacademicdetail?record_id=15
But I want short URL without params in address bar after my form is posted like this:
http://vcred.dev/#personal/myacademicdetail
How it is possible using javascript?
Since you are using a hash in the URL, you could do the following in JavaScript:
location.href = location.protocol + "//" +
location.host +
location.pathname +
location.hash.split('?')[0];
This will not cause a page refresh, as described in this Stack Overflow post: How do I, with javascript, change the URL in the browser without loading the new page?
As far as Zend Framework goes, after you save the record use:
$this->_redirect('#personal/myacademicdetail');
This will tell the user's browser to go to the URL.
I'd do it like this, using PHP:
$referer_host = $_SERVER[ "HTTP_HOST" ];
$referer_uri = explode( "?", $_SERVER[ "REQUEST_URI" ] );
$referer = $referer_host . $referer_uri[ 0 ];
I assumed you might want to see also PHP solution as you've mentioned zend-framework as one of your tags.
I'd simply explode URL like this and then use headers for redirect.

Categories

Resources