This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 4 years ago.
I have a JSP page called index.jsp. I have a Java variable called totalCount inside this page :
<%= int totalCount = getTotalCount();%>
Now I want to use this variable in Javascript section to generate a chart:
<script type="text/javascript">
</script>
How can I pass this Java variable inside ? Thanks.
Just assign the value to your Javascript variable.
<script type="text/javascript">
var count = '<%= totalCount %>';
</script>
But using scriptlets been highly discouraged and shift to JSTL as soon as possible.
Read : How to avoid Java code in JSP files?
As others pointed out, do not create unnecessary hidden elements on DOM. If you really want to use this variable across files declare this variable on top. Even before script includes, so that it avail across all the files.
Example:
<script type="text/javascript">
var count = '<%= totalCount %>';
</script>
<script type='text/javascript' src='js/blah.js'></script>
<script type='text/javascript' src='js/blahblah.js'></script>
Just create a hidden field on your HTML page and access that value using JavaScript.
HTML
<input id='hdn-total-count' type='hidden' value='<%=totalCount%>' />
JavaScript
var totalCount = document.getElementById('hdn-total-count').value;
jQuery (Cross browser compatible)
var totalCount = $('#hdn-total-count').val();
All of the other answers are going to work on inline and on-page JavaScript. But they are not going to work for JavaScript in external files (which are cacheable).
simply you can use
<script type="text/javascript">
var xyz=<%= getTotalCount();%>
</script>
But I dont recommend you to use java codes inside JSP.Please look for JSTL
you can write a code something like this. But it's not a clean way of doing it. Why do you want to do it? You can do it via AJAX calls.
Here is the sample code
<sciprt type="text/javascript">
var myVariable = <%=request.getAttribute("rep");%>
</script>
Related
I have dynamic string javascript tags to execute in head of html.
Tags as follow and this tags is dynamic comes from server as a string and I want to execute this javascript into head of html. How can I best achieve this?
<script async src="/content/js/file1.js"></script>
<script async src="/content/js/file1.js"></script>
<script> alert('Execute'); </script>
You have the right idea; the problem is that you can't combine an external script (using src) with an inline one.
You simply need two different scripts for this, making sure the inline one comes after the reference to an external script:
<script src=""></script>
<script type="text/javascript">
document.getElementsByTagName("script")[0].src = "http://" + location.host + "/content/js/file1.js";
</script>
My knowledge of javascript is medium, sorry if this question seem too newbie
I call a (stripe) script to display the checkout, my 'desc' variable, which is properly in my page, is just not used in the tag. Instead I have 'desc'.
data-description='bonjour' works
var desc = 'bonjour'
data-description=desc does not work
what am I missing here ?
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key=""
data-amount=amount
data-name="<%= current_user.full_name %>"
data-description=desc
data-panel-label="Encaisser"
data-label="Payer par carte"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="fr"
data-currency="eur">
</script>
You don't actually have any Javascript there - what you have is only HTML markup, describing a script tag and its HTML attributes, so you can't use var or anything else particular to Javascript.
<script no javascript here because this is HTML, not JS>
JS can only be used here, in the content of the script tag
</script>
This is a question from a noob in javascript. I tried to find something similar the last two days but I didn't find. I try to pass an html image element to an external javascript file which I include to the rest html code. The javascript fade in and fade out an image. Because I want to use different images everytime, thus I want to have a function in an external javascript file.
What I did so far:
PHP and HTML:
<?php
if ($success){
echo"<link rel='stylesheet' href='js/jquery-ui-1.11.4-smoothness.css'>
<script src='js/jquery-1.11.3.js'></script>
<script src='js/jquery-ui-1.11.4.js'></script>
<img id='tick' src='tick.png'>
<script type='text/javascript' src='js/success_failure_signs.js'>
success_failure(tick);
</script>";
}?>
Javascript file has this code:
function success_failure(tick){
var x = tick;
x.fadeIn();
x.fadeOut(1000);
}
The browser's console doesn't give any error.
It seems that the function success_failure doesn't get the image.
What is wrong on this code and how can I fix it?
Thank you in advance!
You haven't defined tick when you make your function call. Try this...
<script type="text/javascript" src="js/success_failure_signs.js">
</script>
<script type="text/javascript">
var tick = $("#tick");
success_failure(tick);
</script>
EDIT: I've also separated the inclusion of the script and your code to call it into two separate script tags.
maybe passing the image to the script is the wrong way of thinking here.
Most of the time with Javascript for web pages the JS gets the image from the HTML site itself.
You are already including jQuery so look into how to get an element from the page with jQuery.
I want to use a Javascript variable in JSP scriplet of the same page. See example below.
<script language="javascript">
var i=10;
</script >
< % out.println(i); % >
It should print the value of i defined in script tag above. Both script and scriplet are part of the same .jsp page
<div id='out'>
<div>
<script language="javascript">
var i=10;
document.getElementById('out').innerHTML = i ;
</script >
It is not really an answer , because your problem is irrelevant. There is a difference between what gets executed serverside and clientside , and your javascript will get executed clientside , the jsp tags on the server , etc... Either you define and display i in java , or in javascript.
I have in my application layout file an external javascript file witch has several lines of code and at the end runs a function like BooManager.init() no big deal...
the problem is, it is not running the inside code on this javascript file.
this is how i use it:
<script type="text/javascript">
bb_bid = "1615455";
bb_lang = "en-US";
bb_keywords = "iphone4s, apple";
bb_name = "custom";
bb_limit = "8";
bb_format = "bbb";
</script>
<%= javascript_include_tag "http://widgets.boo-box.com/javascripts/embed.js" %>
but it didn`t do anything it was suposed to do...
i`ve tried in simple html file and it works... what am i doing wrong?
NOTE:
the default way in html is this:
<script type="text/javascript">
bb_bid = "1615455";
bb_lang = "en-US";
bb_keywords = "keywords, between, commas";
bb_name = "custom";
bb_limit = "8";
bb_format = "bbb";
</script>
<script type="text/javascript" src="http://static.boo-box.com/javascripts/embed.js"></script>
-- EDIT --
the result generated by rails:
<script type="text/javascript" src="http://static.boo-box.com/javascripts/embed.js"></script>
It's not evaluating the script when loading using the <%= method. I'm not familiar with that syntax, but from the effect, that's what it sounds like. It's treating the script as html rather than code.
jQuery has a script load function that will get a script dynamically from a URL and then eval() it to execute it.
UPDATED WITH SAMPLE CODE
Add jQuery to your app:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
Then use it to load your script:
$.getScript('http://widgets.boo-box.com/javascripts/embed.js');
UPDATE NUMBER 2
I was able to duplicate the problem in this fiddle: http://jsfiddle.net/7x2zT/4/
If what you are trying to accomplish is to get your parameters activated before the script shows the widget - the default one looks like a sidebar, whereas your parameters make it more of a banner, then just make sure you put your parameters above the <script src stuff.
If you must be able to load dynamically, then you're going to have to figure out where the bug lies in the embed code, or if there's some other activation method. That site's documentation doesn't seem to be in English, so I can't help with that.