JSP onmoveover function with java parameter unable to retrieve at javascript - javascript

Right now, i am trying to pass the value from jsp function with java value to javascript. However, i am not able to get the result that i wanted. Instead of getting the arraylist value for eg 4, i'm getting the value like %=list.get(0)%. May i know what's wrong with my code? Is there any syntax error? Your help will be much appreciated. Thanks!
<html>
<head>
<script>
function getValue(value){//expected result would be from arraylist
alert("Values: " + value); // %=list.get(0)% as the alert message.
}
</script>
</head>
<body>
<% ArrayList list=(ArrayList)session.getAttribute("listOfValue"); %>
<html:link action="/hello" onmouseover="getValue('<%=list.get(0)%>');" />
</body>
</html>

Related

JSON Data Not Showing With Jquery

I am learning about JSON and I am having trouble displaying data from an endpoint. This first segment of code works just fine for me.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<div class="mypanel"></div>
<script>
$.getJSON('http://time.jsontest.com', function(data) {
var text = `Date: ${data.date}<br>
Time: ${data.time}<br>
Unix time: ${data.milliseconds_since_epoch}`
$(".mypanel").html(text);
});
</script>
</body>
</html>
Changing the script to this endpoint does not display any data. How can I show this data?
<script>
$.getJSON('https://www.halowaypoint.com/en-us/games/halo-the-master-chief-collection/xbox-one/game-history?gamertags=ice%20cold%20bepsi&gameVariant=all&view=DataOnly', function(data) {
var text = `Gamertag: ${data[0].Gamertag}`
$(".mypanel").html(text);
});
</script>
Looking into it, it seems like you need to authenticate to get this data. If no json is returned, then the callback will not run. That's why if you put anything in the callback, even console.log("HERE"), it will not reach it.
If you want to authenticate, you will need to do an ajax request. $.getJSON is just a wrapper around $.ajax anyway.

Getting a Simple Javascript Function to Work

I'm trying to get a simple javascript function to work. I know enough Javascript to think to myself, "This should work, Why isn't it working!" I'm sure we've all been there before. I have done some research to brush up on my functions and to compare my function to but to no avail, I still can't get this function to work. I'm using Javascript to try to display my name within a span element. Normally this should be easy but, for some reason it just isn't working. This is also done in a .php file and a .html file. Because i wanted to make sure it didn't matter if it was a .php file or .html file. It won't work in either. This is for a php project by the way.
Here's the code
<html>
<head>
<script>
var yourName = "Robin";
function placeName()
{
document.getElementById("myName").innerHTML = yourName;
}
</script>
</head>
<body>
<h1>PHP Basics</h1>
<h2>Hi! My Name is<span id = "myName"></span>
</body>
</html>
Like I said, This should be simple, but it won't work. I'm hoping a new set of eyes (you guys) would be able to point out my rookie mistake. If I need to explain anything in more detail please let me know. And thank you all very much.
You're not running the function. Be careful, only run the function after the span has been created or else it will not work. See this example.
<html>
<head>
<script>
var yourName = "Robin";
function placeName()
{
document.getElementById("myName").innerHTML = yourName;
}
</script>
</head>
<body>
<h1>PHP Basics</h1>
<h2>Hi! My Name is<span id = "myName"></span>
<script>
// run function
placeName();
</script>
</body>
</html>
You have made the body of the function but you haven't call the function so that it will be executed. You can do it like this
<html>
<head>
</head> <body> <h1>PHP Basics</h1> <h2>Hi! My Name is<span id = "myName"></span> </body> </html>
var
yourName = "Robin";
function placeName() {
document.getElementById
("myName").innerHTML
= yourName; }
placeName();
// it won't return undefined try it

Get value into HTML web resource in Dynamics CRM Online

I am trying to get a value from Dynamics CRM into a HTML webresource. I have found this and tried to make the code out of it:
https://msdn.microsoft.com/en-us/library/jj602964(v=crm.7).aspx
It says to use var nameValue = Xrm.Page.getAttribute("name").getValue(); to get it out.
My Code is (the alert is just to try if it gets the right value):
<html><head>
<meta charset="utf-8">
</head>
<body>
<button onclick="getquotenumber()">Try it</button>
<script>
function getquotenumber() {
var getquote = Xrm.Page.getAttribute("quotenumber").getValue();
alert(getquote);
}
</script>
</body></html>
When clicking "Try it" nothing happens! What am I doing wrong?
Thanks, Johannes
As someone already mentioned in the comments, there is no Xrm.Page defined. You can add that by referencing ClientGlobalContext but you would not have gotten any attributes anyway, because this is using Xrm.Page.data under the covers and this is null when you are not inside a CRM form.
https://msdn.microsoft.com/en-us/library/gg328541.aspx
<script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
The simplest thing for only getting this one value is using the parent to get values on the form:
window.parent.Xrm.Page.getAttribute("quotenumber").getValue();
Other options,
pass values to your webresource:
https://msdn.microsoft.com/en-us/library/gg327945.aspx
Or use the OData API:
https://msdn.microsoft.com/en-us/library/gg334279.aspx
Use parent.Xrm.Page.getAttribute("quotenumber").getValue();

ReferenceError when I add C# row to the JavaScript code in view razor page

I have this code in razror view page:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">
Click me</button>
<p id="demo">
</p>
#{
List<UserContact> userContacts = ViewBag.contacts;
String contacts1 = Html.Partial("~/Views/Shared/Contacts.cshtml", userContacts).ToHtmlString();
}
<script type="text/javascript">
var contacts2 = #contacts1;
function myFunction() {
alert(contacts2);
}
</script>
</body>
</html>
when I press button Click me I get this error:
ReferenceError: myFunction is not defined
Why I get this error?
Any idea why I get this error?
Looks like you have some pretty broken HTML. You have assigned the contacts1 server side variable to an HTML fragment:
String contacts1 = Html.Partial("~/Views/Shared/Contacts.cshtml", userContacts).ToHtmlString();
and then you injected it inside a tag:
<script type="text/javascript">
var contacts2 = #contacts1;
function myFunction() {
alert(contacts2);
}
</script>
which totally breaks your markup. Look at the source code of the generated page in the browser and you will see how broken it is.
And how can I fix it?
It's not quite clear what you are trying to achieve so it's pretty hard to say how to fix this issue, but for starters you might try having valid markup and javascript:
<script type="text/javascript">
function myFunction() {
alert('The button was clicked');
}
</script>
It looks like you are trying to load some markup from the server when the button is clicked. You might consider using AJAX if this is the case.
UPDATE:
It looks like you are trying to assign a server side string variable to a javascript variable. This can be done by properly encoding it:
<script type="text/javascript">
var contacts2 = #Json.Encode(contacts1);
function myFunction() {
alert(contacts2);
}
</script>

Call a Javascript function on load of JSP

Can i call a javascript function on load of a JSP file according to a request parameter value?
I want to send a request from my Servelt to the JSP and if the value of a parameter="something" i want to call a specific JS function.
Is that possible? Please send me any relevant example if it is available.
thnks in advance
According to your question i assume this is what you want try it
<%
String text = request.getParameter("parameter");
%>
<html>
<head>
<script>
var text1="<%=text%>";
if(text1.length>0)
{
//do what you want call function or something
}
</script>
</head>
<body>
</body>
<html>

Categories