Call js function - javascript

<div id="picker-updatedDate{%= ID %}" class="bordForTdPickerTables cellPadding cellNotEditable lastUpdateDIV">{%= ConvertDateFromMSajax2JSFullDateStr(EnteredWhen) %}</div>
But the function ConvertDateFromMSajax2JSFullDateStr doesn't get called.
How can I call the function?

Assuming ConvertDateFromMSajax2JSFullDateStr is client side JavaScript function and you want to pouplate the div with its output, passing server side argument, first have this code in your page:
<script type="text/javascript">
window.onload = function() {
var oDiv = document.getElementById("picker-updatedDate<%=ID%>");
oDiv.innerHTML = ConvertDateFromMSajax2JSFullDateStr("<%=EnteredWhen%>");
}
</script>
Then correct syntax for the div itself is:
<div id="picker-updatedDate<%=ID%>" class="bordForTdPickerTables cellPadding cellNotEditable lastUpdateDIV"></div>
Make sure to have the code assigning values to ID and EnteredWhen variables before the above.

Your question is unclear and your code is syntactically incorrect. As others have pointed out, there is no javascript function provided in your example.
ASP delimiters are "<% %>" not "{% %}"
To "call" a function in ASP you can use :
<% call MyFunctionName() %>
However, by using the explicit call statement, no value is returned from the function.
if the function is to return a value then omit the call statement:
<%= MyFunctionName() %>

Related

Javascript InnerHTML is a scriplet code displaying as a text

I am using javascript innerHTML to post some data from javascript. Please find the below code:
var innerHTML='<% SomeClass.getSomething %>';
document.getElementById("something")=innerHTML;
HTML:
<div id="something"></div>
Now i am able to see the output as <% SomeClass.getSomething %>. But how do i render it to show the value of getSomething. Please Help.
The SomeClass is defined in jsp where the html is there.
Assuming your code is in a JSP file I think you want the following
var innerHTML='<%= SomeClass.getSomething %>';
The <%= tells the JSP to output the expression in place.
Try
var innerHTML='${SomeClass.getSomething}';
If you are passing it througn model.

How to pass or use variable from GSP in Javascript

I have below line in my grails gsp file.
<div class="pagination">
<g:paginate total="${lotCount ?: 0}" />
</div>
I want to pass lotCount value to one of my javascript named area-switcher.js file to further use it. How can I do this?
I tried to refer one suggestion from How to pass a value directly from a controller to a javascript file in Grails
where I do below in my gsp
<g:javascript> var theId = ${lotCount} </g:javascript>
and try below in my js file for testing
alert(theId);
but it doesn't work.
Got error like ReferenceError: theId is not defined.
Please help.
Use a hiddenField:
<g:hiddenField name="lotCount" value="${lotCount}" />
<div class="pagination">
<g:paginate total="${lotCount ?: 0}" />
</div>
Your solution should work.
Just ensure that in source of generated html page line
<g:javascript> var theId = ${lotCount} </g:javascript>
is placed before line which includes area-switcher.js
<script type="text/javascript" src="area-switcher.js">
Besides that, there are two more options to pass some value from .gsp to javascript file (examples use jQuery):
By using of data attribute. For example,
gsp (here div, span, input, other tags could be used):
<div id="countElem" data-count="${count}">
js(jQuery used here):
var count = $("#countElem").data('count');
As was mentioned in another comments, by using of hidden field:
gsp:
<g:hiddenField name="countElem" data-count="${count}"/>
js(jQuery used here):
// hidden field will have name and id equals to countElem
var count = $("#countElem").val();

Calling JavaScript function into a P tag

I would like to call the following javaScript function so it gets outputted to a HTML P tag, but am not sure how to do this without explicitly calling the function in the HTML file.
I do not want to do this...
<p class="showcode">
<script type="text/javascript">
wise_words();
</script>
</p>
I would like to keep the javaScript code all in one js file.
I have tried it this way but this does not seem to work...
document.getElementById("showcode").innerHTML = wise_words();
I would really appreciate any help as to what I am doing wrong.
Here is my code... http://codepen.io/anon/pen/qfLdE I would like to have the generated text get outputted inside the grey box.
You should call the function in an onload handler, so that it is executed after the DOM has been constructed:
window.onload = function() {
document.getElementById("showcode").innerHTML = wise_words();
}
Another problem is that your wise_words() function is using document.write (please don't use document.write) instead of returning a value. You need to return a value:
var retText = wiseText[nextVal][0];
nextVal += 1;
writeCookie("wisewords", nextVal.toString(), 33);
return retText;
Try following using Jquery:
$(".showcode").html(wise_words());
NOTE: Assuming your function returns the HTML/text.
<p class="showcode">
<script type="text/javascript">
document.write(wise_words());
</script>
</p>
or:
<body onload="document.getElementById('showcode').innerHTML = wise_words()">
<p id="showcode">
</p>
</body>
(note id instead of class).

ASP.net MVC insert JavaScript function result in view

I'm trying to do the following thing: <div>javascript:processData(Model.Property)</div>, where "processData" is a function that returns a string. How can this be done?
(This obviously is not working, it just returns the string "javascript:processData(Model.Property)".)
You can call the javascript method on jquery document ready and set the result to that div using html method.
#model YourSomeModelWithThatProperty
<div id="thatDiv"></div>
<script type="text/javascript">
$(function(){
var thatDataString=processData("#Model.Property");
$("#thatDiv").html(thatDataString);
});
</script>
Read the following question :
Using Razor within JavaScript
You need razor text markers:
http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
<script type="text/javascript">
function processData(property) {
// all kinds of stuff...
// return result;
}
</script>
...
<input type="button" onClick="return processData(<%: Model.Property %>); />
If you're using Razor syntax, replace <%: Model.Property %> with #Model.Property.
As for displaying the result, use the javascript to put the value somewhere on the page (such as the text of an <a> element or an alert).

js make all the url parameter's value have an " "

I have an iframe which includes src like this:
<iframe id="frame1" src="/jsp/transfer/a.jsp?isChange=true&bizId="+bizId></iframe>
bizId is a number. For example:
src = "/jsp/transfer/a.jsp?isChange=true&bizId=10"
I notice that Javascript will make put bizId's value in quotes: "10", "null", etc. I want to get the actual numeric value, not a string. Why is it represented as a string? What should I do?
If I understood your problem right (especially your last comment), you need to do this:
<iframe id="frame1" src="/jsp/transfer/a.jsp?isChange=true&bizId=<%=bizId>"></iframe>
Explanation:
<%= variable > - is a JSP syntax for inserting variables from JSP context into rendered HTML. This code (<%= variable >) will be replaced fully by contents of variable.
Added: (in response to comment)
If you need to put some variable into JavaScript file which is included from your original JSP file, you wont be able to use <%= variable > syntax in it. However, here is what you can do:
[yourjsp.jsp]
<script>
var bizId = <%=bizId>;
</script>
...
<script src="yourjavascript.js"></script>
[yourjavascript.js]
function someMethod() {
alert(bizId);
}
Basically, JSP code will be replaced, and you will define a global javascript variable called bizId containing the value of server-side bizId. Then, any other javascript code can use that variable.
in JS :
window.frames["myIframe"].src = "/jsp/transfer/a.jsp?isChange=true&bizId="+bizId;
you Can't add to the src something like +param.
option 1 ) via server side
option 2) via Js - change the SRC.
edit
<iframe name="myIframe" id="frame1" src=""></iframe>
in the bottom of the page :
<script type="text/javascript">
var bizId=444;
window.onload = function() {
window.frames["myIframe"].src = "/jsp/transfer/a.jsp?isChange=true&bizId="+bizId;
};
</script>

Categories

Resources