I feel like I already know the answer to this is going to be "not possible" but just in case.
Let's say I have this javascript function used to read .net webform field's value:
function getById(theId) {
return document.getElementById(theId).value;
}
which I can call like this:
getById("<%=txtField1.ClientID%>");
Ok, that works fine.
But it is a given that .ClientID is always going to be in there, which means this function could be whittled down, but only if it is possible to represent the form field as a variable by itself. Something like this:
function getById(fieldName) {
return document.getElementById(<%= + fieldName + .ClientID%>).value;
}
to be called like this (much cleaner)...
getById("txtField1");
Is this possible?
Well yes and no/maybe.
Yes Part:
JS order of operations supports the ability to append strings before the get element call. For example if I had a textbox with id "searchTerm" then I could do this in js and be absolutely fine:
var check = document.getElementById('search' + 'Term').value;
NO Part: unless webforms differs significantly than what I remember way back when, that original function you have there is created to specifically get values when js is called inline and is about as optimized as you are going to get for that action. Once the page is loaded all of those server side variables will no longer be available for javascript and you would have to use the true client side elements IDs. Once workaround I suppose is to add onClick action to pass the client side ID such like so
<input type="text" onClick="WhatIsLove(this.id)" value="BabyDontHurtMe" id="Sing">
Related
I'm trying to append a Html.ActionLink with jQuery like this
a.append("<li>#Html.ActionLink("e-TCGB","Inbox","Folder",new { Type = "1",DocumentTypeId = "3" },null)+"</li>");
and it is giving errors.
Being very inexperienced in javascript and jQuery I don't know if the error is because of wrong string parameter or because of doing something very wrong.
My guess is I'm making an escape character mistake but as I said, I don't know if what I'm doing is possible too.
'Razor is compiled at runtime - meaning its already done doing it's thing before your jQuery code is executed.
You can simply use a hyperlink though:
var li = $('<li>');
var link = $('<a href="/folder/inbox/?type=1?documenttypeid=3">e-TCGB</div>');
li.append(link);
a.append(li);
UPDATE:
Above, you can see two examples of generating elements using jQuery. The first is shorthand for generating a new <li> element:
$('<li>');
The second is generating a hyperlink tag. If you want to add attribute information you can do so in a number of different ways however I prefer to just write the tag out in long form when generating the element:
$('<a href="/folder/inbox/?type=1?documenttypeid=3">e-TCGB</div>');
#Html.ActionLink is a helper method in MVC designed to be used in the Razor views. It is executed on the server and processed as the Razor view is rendered to HTML.
jQuery is a JavaScript library that is used on the browser so execution here happens after the HTML has been received by the browser.
To recap, it is not possible to execute c# code (ActionLink) on the browser because it is a .net based server side method.
I'm trying to set a component's text based on a bean's value. I'm using jquery for this because the text changes depending on certain conditions.
So, the jquery code looks like this:
window.onload =function(){
$('.pnx-inline-input').on("change keyup paste", function(){
var saveText = #{extra.Active_Save};
$('.save-button .pnx-btn-text').html(saveText);
});
The Extra bean handles the localization. So, let's say that the locale is France, and the text is Enregister. The thing is that when rendered the page, the code segment looks like this
window.onload =function(){
$('.pnx-inline-input').on("change keyup paste", function(){
var saveText = Enregister;
$('.save-button .pnx-btn-text').html(saveText);
});
Of course, Enregister is not defined anywhere, and this causes an error. I need to have to code look like
var saveText = "Enregister";
for this to make sense.
How can I make this happen? Thanks!
JSF is in the context of this question merely a HTML code generator. Just write down those quotes yourself in the HTML template. They are part of generated HTML output, not of the Java variable. You know, JavaScript doesn't run together with Java/JSF (i.e. it's not server side). Instead, it runs together with HTML (i.e. it's client side).
var saveText = "#{extra.Active_Save}";
Note that you still need to take into account that the value is properly JS-escaped, otherwise the whole thing would still break in JavaScript side if the string itself contains doublequotes or other special characters in JS such as newlines. The JSF utility library OmniFaces has an EL function for the very purpose, the #{of:escapeJS()}:
var saveText = "#{of:escapeJS(extra.Active_Save)}";
You can of course also homegrow your own based on e.g. Apache Commons Lang StringEscapeUtils.
I have googled it several times but i can't get a solution. I want to make javascript function call from the bean class in jsf and i get that using the following code.
RequestContext.getCurrentInstance().execute("handleResize()");
and is workign fine. But I want to give two parameters to that function height and width. How can it be done ? please help
You seem to fail to grasp the fact that in the context of Java/JSF, all the HTML, CSS and JavaScript code are merely plain vanilla Strings and you seem to expect that HTML/CSS/JS somehow magically runs inside Java/JSF code. This is not true. Java/JSF is a HTML/CSS/JS code producer, not executor. The webbrowser retrieves them all as one big String and then parses and executes it.
If you want to invoke a JS function with parameters supplied, like so when you would do in real JS code:
handleResize(500, 300);
And you have those values as Java variables, then you just need to make sure that you write Java code in such way that exactly the above String is produced (again, this is just Java code, no JS code):
String call = "handleResize(" + w + ", " + h + ")";
You can verify beforehand by printing it to the stdout/logger:
System.out.println(call);
It must print exactly the desired valid JS function call syntax handleResize(500, 300);.
If it does, then just pass that unmodified to RequestContext#execute().
RequestContext.getCurrentInstance().execute(call);
is it possible to get the ID assigned to User Control from the control using javascript or jquery.
Thanks
What ASP.NET normally does is prefix your control's ID with a string that it uses to determine where in ASP.NET's control tree your actual control resides.
With that in mind, what I normally do is to use jQuery's 'ends with' selector to get the full ASP.NET-parsed ID at runtime.
Something like:
// get a handle on your original control
var myControl = $('[id$="<myOriginalId>"]');
// and then access it's properties
var myRuntimeId = myControl.eq(0).attr('id');
As you can most probably imagine, that's not going to cut it when you've got UserControls with the same ID used in different places of the form. I just jump in and put in some tweaks here and there (probably with using the .eq() function) to suit my business need.
You could put a class on the usercontrol, and then use something like $(".myUC").attr("id")
This might help you to look at it from a different point of view:
In .Net you can get the generated ID by using myControl.ClientID.
If you put that in a javascript variable - I know it's not neat - you can then easily fetch it.
<!--mypage.aspx-->
<script>
var myIdVar = "<%=myControl.ClientID%>";
if(myIdVar == "foo")
{
alert("bar");
}
</script>
I am trying to populate multiple form fields with JSON data after a user makes a choice from a select box. I am very new to jquery so sorry if I am getting something elementary wrong. I don't want to include the JSON in my html because it will be changing often and it is a very large file. Here is what I have:
<script type="text/javascript">
$.ajax({
url: '../includes/json/data/abbc.json',
success: function(data) {
$("#rig").html("<option >--select--</option>");
$.each(rigdetaillist.rigs,function(){
var rigName=this.rig;
$("#rig").append("<option value=" +rigName + ">" +rigName + "</option>");
});
$("#rig").change(function(){
var rigValue=$(this).val();
$.each(rigdetaillist.rigs,function(i){
var rigName=this.rig;
if (rigName==rigValue){
$(".rigdetail").val("");
$.each(rigdetaillist.rigs[i].rigdteails,function(i){
var rigdetailName=this.rigdetail
$(".rigdetail").eq(i).val(rigdetailName);
});
}
});
});
}
});
</script>
There are a few things here that you want to consider, one of the major things being that you want to do as little DOM manipulation as possible, as its one of the major causes of performance issues.
Unfortunately, I don't have the time to rewrite your code and give you what you should write. Although I do have the time to explain to you what you need to consider and hopefully it'll lead you down the right path :-)
(1) Your data var in success: function (data) { isn't being used. I'm assuming you mean it to be used for the rigdetaillist, most likely something like var rigdetaillist = data['rigdetaillist']; depending on your JSON. In either case, data is your json return value, of which you aren't referencing at all...which you probably need to. :-)
(2) As I said earlier, you want to do as little DOM manipulation as possible. So you probably will want to either pull out the #rig and cache it in javascript (to be put back into the DOM later), or create a new $(<script>) obj and then copy its html into $('#rig')'s when you are done. I would suggest creating the new script tag as detaching is a very nice feature of jquery, but can have its problems (such as when you go to append it back in, it appends at the bottom of the container instead of where you originally had it).
(3) The change function is being created on every json request. There is more than likely a way to globalize that so you don't have to create a new change function every time. You could possibly store the necessary information in a global variable and just reference that variable in change function, or I'm sure you could do some other really cool scope tricks, but that would be the simplist.