How to use JavaScript inside EL expression (ui:param with value) - javascript

I want to be able to pass a value of javascript function to an ui:parameter like this:
<ui:param name="paramName" value="someWidget.function()"/>
But this obviously does not work (it thinks that: someWidget.function() is a string parameter).
If I try:
<ui:param name="paramName" value="#{someWidget.function()}"/>
It thinks that someWidget is a bean name.
Is there any way to place there a value of javascript function on a widget? If not, what would be ideal way of including a value of javascript function in a parameter in JSF?

There's a major conceptual misunderstanding going on. JSF runs in webserver and produces a HTML page with therein some CSS/JS code which get delivered to the webbrowser. That HTML page with CSS/JS in turn runs in webbrowser. JS thus definitely doesn't run in webserver "in sync" with JSF somehow. To JSF, JS is like HTML just part of template text which it needs to produce.
You need to look for the solution in a different direction. As the concrete functional requirement is unclear, I can't give any hints in the right direction nor kickoff examples. To the point, you should perform it in Java instead of JS, or to let JS during page load send an ajax request with the desired variable as request parameter.

Related

Calling a function in a JavaScript file with Selenium IDE

So, I'm running these Selenium IDE tests against a site I'm working on. Everything about the tests themselves is running fine, except I would like to do a bit of clean-up once I'm done. In my MVC3 Razor based site, I have a JavaScript file with a function that gets a JsonResult from a Controller of mine. That Controller handles the database clean-up that Selenium IDE otherwise couldn't handle.
However, I'm having a hard time finding any sort of documentation on how to do this. I know I can do JavaScript{ myJavascriptGoesHere } as one of the Values for a line in the test, but I can't seem to find a way to tell it to go find my clean-up function.
Is it even possible for Selenium IDE to do this sort of thing?
If it comes down to it, I can just make a separate View to handle the clean-up, but I'd really like to avoid that if possible.
Thanks!
If you want to execute your own JavaScript function that exists in your test page from Selenium IDE, you need to make sure you access it via the window object. If you look at the reference for storeEval for instance, it says:
Note that, by default, the snippet will run in the context of the
"selenium" object itself, so this will refer to the Selenium object.
Use window to refer to the window of your application, e.g.
window.document.getElementById('foo')
So if you have your own function e.g. myFunc(). You need to refer to it as window.myFunc().
This can be very handy for exercising client-side validation without actually submitting the form, e.g. if you want to test a variety of invalid and valid form field values.
If you use runScript, that should already run in the window's context.
This works for me.
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("myJavascriptGoesHere");
Make sure your javascript works first before using it here!
Actually to access your page javascript space, you need to get the real window of your page : this.browserbot.getUserWindow()
See this statement to get the jQuery entry point in your page (if it has jQuery of course ^^ )
https://stackoverflow.com/a/54887281/2143734

Struts tags in Javascript

What would you consider a better practice?
Would you do (in javascript):
var aaa = <s:property value="myValue"/>;
or rather (in a jsp file):
<s:hidden name="myValue" id="myValue" />
with (in javascript):
var bbb = document.getElementById("myValue").value;
Well that depends I think. You can't use struts2 tags in JS files unless you save them as .jsp. In case you do this or you have a JS code in your page, I personally doesn't have a problem with the first option, since the tags will be processed back at server. I use this often when there is a simple assignment.
I don't like the second option cause it makes the code a little ugly and might cause some maintenance problems.
To sum it up it's a better practice to separate your JS and server side codes. If you find yourself mixing things up you might want to look at your design somewhere that could change.
Again that depends on how you prefer to do it.
You can achieve the desired results with both way.only thing matters is individual taste and preferences.
i would not like to mix up the things and will go with the second way using a hidden field.for me it serve 2 purposes.
separating my struts2 tag from the JavaScript.
More over i can write a simple clean JavaScript code with standard JavaScript method rather mixing up the things
There are many ways, including what you've shown.
You may also:
Pass JavaScript files through the JSP processor
Use a different templating language for dynamic JS (like FreeMarker)
Emit a JSON object evaluated before your JS files are included (e.g., the contents of a hidden div are evaluated purely for JS side-effects)
Make an Ajax call for the same JSON object on page load before your functionality fires
Probably a few more. I don't prefer hidden fields unless they're in a form that isn't submitted (like a hidden, separate form) unless it's very clear which form values matter and which don't.

AJAX Script Loading

I am currently writing a program that uses AJAX to load a form for editing objects on a website. I have found a similar question at Loading script tags via AJAX, but it doesn't really satisfy the needs of the program.
The ajax returned is a pre-built set of elements in a form, and when certain areas are called, say, a TinyMCE textarea (which it is), it returns a set of script tags built into the text.
So my question is, is it possible to run through the script tags that have been put in the div and run them?
Plus, I want to avoid using jQuery as it could be running on any number of platforms.
Yes, you can add the incoming html and scripts to the dom, then search the dom for any script tags. You would then eval the scripts and could ignore any jQuery script tags if you wish.
However:
This sort of solution tends to be quite brittle.
It would be much better and more stable for you to modify the Ajax payload into separate html and javascript scripts. That way your Ajax handler would be able to handle them directly without trying to separate them.
Added
Re: how to send back the html and javascript parts: you can either make separate Ajax calls, or return an JSON object that includes both parts. Eg:
{"js": "<the js part of the response>",
"html": "<the html part of the respons>"}
Use a json library on your host system to take care of the issue of escaping any quotes or other json special characters in either the js or html values.
Returning both the html and js at once saves an Ajax call (which can be significant) and will usually simplify your code quite a bit vs two calls.
I use this technique in production and it works well.
Do you mean you return a js script from the ajax and want to run it??If so, you can use the eval function.

Adjust static value into dynamic (javascript) value possible in Sharepoint allitems.aspx page?

<SharePoint:SPDataSource runat="server" IncludeHidden="true" SelectCommand="<View><Query><OrderBy><FieldRef Name="EventDate"/></OrderBy><Where><Contains><FieldRef Name="lawyer_x0020_1"/><Value Type="Note">F. Sanches</Value></Contains></Where></Query></View>" id="datasource1" DataSourceMode="List" UseInternalName="true"><InsertParameters><asp:Parameter DefaultValue="{ANUMBER}" Name="ListID"></asp:Parameter>
SharePoint WSS 3.0
This codeline is just one line of the allitems.aspx of a sharepoint list item (SharePoint created it himself). It only displays items where lawyer 1 = F. Sanches. Before I start messing around with the .ASPX page I wonder if it possible to change F. Sanches (in the code) into a dynamical variable (from a javascript value or something else that can be used to place the javascript value in there dynamically). If I put any javascript code in the line it will not work.
P.S. Ignore ANUMBER part in code.
Let say to make it simple I have javascript variable like this (now static but with my other code it is dynamic). It would be an achievement if it would place a static javascript variable.
<SCRIPT type=text/javascript>javaVAR = "P. Janssen";</script>
Eventually I would like to filter with OR operator
If Yes --> how?
If No --> Thank you!
The easiest way to add a filter to a standard list view is to use query string filters - add
?FilterField1=Lawyer&FilterValue1=F. Sanches
to the url. If you are working with a note field, it probably won't work directly, but you may find it easier to set up a field that does work than to make the server side chages you would need to modify the query directly.
Another approach that might work would be adding filter web parts to the page - I haven't worked with those much myself, but in theory they should be able to do what you want.
I don't think JavaScript will work since JavaScript is client side and SPDataSource is server side.
However, you should be able to use a control via ControlParameter. There are good examples here and here. Once the parameters are working, OR operators will definitely be supported within the CAML of the SelectCommand.

Help me to understand <script src="some.js?param1=one;param2=two" />

I observed chunks like below sometimes on web pages. So i am curious to know what this really does? or why its written in such a way?
<script src="somefile.js?param1=one&param2=two" />
i can only make out following few intentions behind it
Its not page URL (i mean .aspx/.php/.jsp etc.) so its not hacking kind of code where user can add code like this to pass data without getting users attention as its tag which does not render on UI OR implementing old type of AJAX alternative
This kind of URL param are useful if user do not wish the JS file (any other resource like image) to get cached. This can be quick way to manage caching
But i am unable to figure out following
Looks like page URL parameters but are these parameters anyway readable in JavaScript file and have some additional utility?
Do these parameters have any extra role to play here ?
What are the other possible practical scenarios where code like this can be/is used?
So please provide some inputs related with the same
Thanks,
Running Non-JS Code within .js Extensions
In cases like this, that source .js file might (given proper server-configurations) actually have PHP/.NET code within it, which can read those appended values.
As you said, Avoiding Cache...
Additionally, people will at times append a random string at the end of their referenced elements to avoid loading cached data.
The URL having '.js' means nothing. It could still be handled by a server-side script like an ASP or PHP.
Either the javascript file is not static (it is generated by the server based on the parameters in its querystring)
OR
In the JavaScript file itself, you can have it check its own querystring parameters (not just that of the page, but that of the javascript source url).
OR
(This doesn't exactly match your scenario, but) you can also add parameters at the end of image and script urls as a way of versioning. The version with the url="somescript.js?V=3" will be cached by the user until the page then changes and the url is not="somescript.js?V=4". The file will be replaced by the version on the server no matter what the browser setting may be.
My guess (without looking at this specific case) is that the javascript file is reading its own querystring. I have done this, and its very helpful.
Looks like page URL parameters but are these parameters anyway readable in JavaScript file and have some additional utility?
Yes you can read them in JavaScript, Scriptaculous uses that approach for loading modules, eg:
<script type="text/javascript" src="scriptaculous.js?load=effects,dragdrop">
</script>
Do these parameters have any extra role to play here ?
What are the other possible practical scenarios where code like this can be/is used?
That can be also used for server-side script joining and minifying, of course using some url rewriting technique to have the .js extension, and as you say, it's a common technique to add timestamp parameters to break the browser cache.
It can be used for three different reasons:
1) To generate the JavaScript file in the server depending on the parameters;
2) To avoid caching;
3) To pass parameters to JavaScript itself
An example of this in practice would be a server side handler for somefile.js that uses the parameters (names of other scripts) to determine which scripts are actually required and combine/minify them, returning them as a single somefile.js script file.

Categories

Resources