Pass an array from .jsp file to .js file [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to transfer java array to javaScript array using jsp?
After googling I am still not able to pass array from .jsp file to js file.
Can somebody help me out?
In my .jsp file i have an array and I wan to call a function in .js file which accepts this array. How to call this function?

By passing an array from .jsp to js file I guess you mean your Javascript script needs to "call" a script return by a JSP file. And that script contains a function that returns an array?
If so have you looked at returning JSON from your JSP?

For simple arrays, you can convert the array to a comma-delimited string using an implode function, pass the string to javascript, and then use split(str) in Javascript to get an array back. More complicated arrays may cause issues with this.

Related

Is there any better way to pass javascript variable as php variable? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass JavaScript variables to PHP?
(16 answers)
Closed last month.
I am playing with PHP and Javascript. Also I am trying to find all possible ways to pass javascript variable into php variable in the same php file. So far, I have found two possible ways. One is well known to everybody which is ajax (i,e calling another php file). But it could not solve my problem easily. Another solution that I have figured out is utilizing COOKIE/SESSION. Here is my simple code example: It will alert 5 to 10. Javascript variable value 5 is passing inside php variable =>
<div id="one">5</div>
<script>
var vall = document.getElementById("one").innerHTML;
document.cookie = "val="+vall;
alert('<?php for($i=$_COOKIE["val"];$i<=10;$i++){echo $i;} ?>');
</script>
Do you have any other alternate or better solution?

How to pass variable value from php to external JavaScript file? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
I have a value sitting in my database which i want to use in an external JavaScript file. The JavaScript file is being referenced in the the html code using <script src="/thanks/thanks.js"></script>
Can anyone please help to figure out how i can either pass the value from php tp JavaScript or how i can access the database from the JavaScript?
The simple way is to use the <script> tag and echo the value you want to.
<?php
echo "<script>var parse = $value;</script>";
?>
Now you can use your value in javascript (parse variable). But if you want to have it detailed...
here is the duplicate you didnt search for: How to pass variables and data from PHP to JavaScript?

Fetching data from a json file using variables javascript [duplicate]

This question already has answers here:
How do I access a JSON object using a javascript variable
(4 answers)
Closed 8 years ago.
My question is pretty simple. I have a json file stored and I fetch it using a simple xml request and store it for later use in the localstorage. Now What I need to do is that when the user selects something from the select tag, I fetch the value of the tag and use it get the data from json.
For ex:
database.Class.Teachers."The value from the select tag".age
Is this possible with javascript or similar workaround?
Ok i got the answer. Thanks.
link for the working code i found.
How do I access a JSON object using a javascript variable

Passing a PHP array via Javascript to another PHP script

I may be going about this the hard way, as I modeled my code after an older article: populating triple dropdown list but I have found I don't need to query the database, because all the information is already neatly stored for me in a PHP array used earlier in the script.
I have a PHP array already created ($events):
<?php foreach($events as $event) {
Now I have to pass the events array to another file for processing:
<select name="search_month" onChange="getMonth('filter-leaderboard.php?month='+this.value)">
I'm just not sure how to pass it via Javascript.
How do I go about doing that?
Thanks!
Looking at your question, it appears you're trying to get the month using AJAX?
If so, it'd probably be better to post the array as the body rather than doing a get request.
You can use the json_encode function (http://au2.php.net/json_encode) to convert the array into a JSON object so you can work with it in JavaScript.

Convert JavaScript file to JSON [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parse JSON in Android
I am writing an Android app where I'm getting a .js (JavaScript) file from a url and I want to read its contents. Is there any way to convert this file to a JSONArray or JSONObject? Or a direct way to parse the .js file itself?
If you mean can you create a data structure out of raw JavaScript, then no.
You can't convert a string of arbitrary JavaScript to JSON except in the degenerate sense that the entire string is a valid JSON item of type 'string'. This is because the syntax of JS covers a much larger domain than JSON. For example, what kind of JSON structure would you expect to represent the following JS?
while (true) { }
If your intent is to traverse the JS and pull data structures out of it, you're probably going to need something like a full JavaScript parsing engine.
If on the other hand you've phrased the question badly and the '.js' file you're fetching is really a JSON file, then the question is answered in the marked duplicate.

Categories

Resources