361 Lotus blogs updated hourly. Who will post next? Home | Downloads | Events | Pods | Blogs | Search | myPL | About 
 
Latest 7 Posts
Multiple Chrome Incognito browsers share the same data
Sun, Nov 23rd 2014 157
x$ – now a part of XPages Extension Library
Tue, Nov 18th 2014 230
Got non-XPages design elements? You really should use WebContent.
Mon, Nov 17th 2014 252
XPages and Bluemix: How to create a Websockets message without being connected to the application
Sun, Nov 16th 2014 248
One way to make a responsive XPages viewPanel
Tue, Nov 11th 2014 299
Creating an XPages Websockets chat client using Bluemix
Sun, Nov 9th 2014 1136
Finding your localStorage values – Chrome Developer Tools
Thu, Nov 6th 2014 146
Top 10
Creating an XPages Websockets chat client using Bluemix
Sun, Nov 9th 2014 1136
Create your own Watson Q and A example with Bluemix, Webstorm and Jazz Hub
Sun, Oct 12th 2014 519
Integrating the Bluemix Watson Translation service into an XPages application
Sun, Oct 19th 2014 466
Binding jQuery code to an XPages partialRefresh using DOM Mutation events
Wed, Oct 29th 2014 325
One way to make a responsive XPages viewPanel
Tue, Nov 11th 2014 299
Arguably the most dangerous thing you could ever do in XPages – let other people use your eval() in their SSJS
Tue, Jun 18th 2013 289
Oh duh, that is why Node Package Manager (npm) is so cool
Tue, Oct 14th 2014 275
Simple examples of how Google Developer Tools can aid Mobile Development
Thu, Oct 30th 2014 258
Got non-XPages design elements? You really should use WebContent.
Mon, Nov 17th 2014 252
XPages and Bluemix: How to create a Websockets message without being connected to the application
Sun, Nov 16th 2014 248


XPages CSJS timing issue – what is ‘this’ ?
MarkyRoden    

Problem

The JavaScript object represented by ‘this’ does not appear to be as expected.

The problem is caused because of the way that XPages adds events to DOM objects through the <xp:eventHandler>

The Situation

I wanted to pass the id from a button to a function. Outside of XPages I would normally acheive this by passing a reference to the button(this) to a function (saySomething in this case) and then extracting the id from the object passed:

function saySomething(obj){
	alert("This is my object - "+this
		+"nnThis is my id - "+
		this.id)
}

Describing 'this' when passed from a button to a function

Describing 'this' when passed from a button to a function

	<button id="markyButton" onclick="saySomething(this)"></button>

In this case I am passing the “this” object to the function.

this

is an object reference to the DOM element which it was generated from – for some serious stuff on ‘this’ check out this article

http://www.quirksmode.org/js/this.html

XPages fail :(

However when I add the JavaScript to the XPages button and add the code through the GUI Designer interface

Adding JavaScript through the XPages GUI

Adding JavaScript through the XPages GUI

I do not get the expected result when I click on the button……

Not what we expected

Not what we expected

The Explanation…

The reason this happens is because of the way that XPages assigns events to objects in the webpage. If you have ever looked at the source of an XPages you will almost certainly have seen a whole load code like this at the end of the page

XSP.addOnLoad(function() {
XSP.attachEvent(".....
}

and this is how the XPage adds the event to the object – in the case of this example the XPages creates our button in the HTML but as you can see there is no onclick event initially assigned to it

		<button class="lotusBtn" type="button" name="view:_id1:_id2:_id37:button1" id="view:_id1:_id2:_id37:button1">Click This</button>

and then at the bottom of the web page source code we can see the assignment of the event after during the “addOnLoad” which means after the page has loaded.

<script type="text/javascript">

function view__id1__id2__id37__id40_clientSide_onclick(thisEvent) {
alert("This is my object - "+this+"nnThis is my id - "+this.id)

}

XSP.addOnLoad(function() {
XSP.attachEvent("view:_id1:_id2:_id37:_id40", "view:_id1:_id2:_id37:button1", "onclick", view__id1__id2__id37__id40_clientSide_onclick, false, 2);
});

</script>

What is happening?

The XPage is adding the function “view__id1__id2__id37__id40_clientSide_onclick” to the “onclick” event of the “view:_id1:_id2:_id37:button1″ DOM element (our button in this case).

The nuances of this are subtle but important – the code is not adding “alert(whetever)” to the onclick event it is adding a function call to the onclick event.

So. when the button is clicked the function view__id1__id2__id37__id40_clientSide_onclick executes it’s code and in that situation ‘this’ no longer refers to the button.

The difference is clear when we look at the resulting code side by side

<button value="or Click this" id="button3" onclick='alert("This is my object - "+this+"nnThis is my id - "+this.id)'></button>

or

<button value="or Click this" id="button3" onclick='view__id1__id2__id37__id40_clientSide_onclick()'></button>

“this” is never passed to the view__id1__id2__id37__id40_clientSide_onclick() function and therefore any reference to the button is lost.

When the function executes – “this” refers to the function (which isn’t an object) and therefore we get the window (the webpage) as a failover.

The solution

If you need to do something like this then add the onclick event to the source pane.

Here is the XML markup for the XPages generated event (which fails me)

		<xp:button value="Click This" id="button1">
			<xp:eventHandler event="onclick" submit="false">
				<xp:this.script>
					<![CDATA[alert("This is my object - "+this+"nnThis is my id - "+this.id)]]>
				</xp:this.script>
			</xp:eventHandler>
		</xp:button>

and the XML markup  for the manually added onclick event (which works)

		<xp:button value="or Click this" id="button2"
			onclick='alert("This is my object - "+this+"nnThis is my id - "+this.id)'>
		</xp:button>

The demonstration

Here is a simple demo of the problem and the solution




---------------------
http://xomino.com/2012/04/09/xpages-csjs-timing-issue-what-is-this/
Apr 08, 2012
17 hits



Recent Blog Posts
157


Multiple Chrome Incognito browsers share the same data
Sun, Nov 23rd 2014 7:15p   Mark Roden
In a previous post I talked about how going porn-mode on your browser is great for a developer. What I didn’t realize until today is that incognito mode is not as unique as I want it to be. We use localStorage in one of the applications we are developing and I had issues when I was opening up multiple Chrome Incognito mode browsers, assuming they were unique….I wanted to compare separate instances in separate windows Mr. Genius (Toby) pointed out to me that localStorage is shared a [read] Keywords: applications
230


x$ – now a part of XPages Extension Library
Tue, Nov 18th 2014 9:16p   Mark Roden
I am very flattered to find out that not only is my x$ OpenNTF xSnippet being used more widely than I realized (over 600 downloads). It now being used in the latest release of the OpenNTF Extension library. If you look here – http://bootstrap4xpages.com/xsp/.ibmxspres/.extlib/responsive/xpages/js/xsp-mixin.js and search for it you will find //jQuery selector that works with XPages IDs //See - http://openntf.org/XSnippets.nsf/snippet.xsp?id=x-jquery-selector-for-xpages function x$(id [read] Keywords: xpages community openntf
252


Got non-XPages design elements? You really should use WebContent.
Mon, Nov 17th 2014 7:20p   Mark Roden
Recently I was able to help explain an issue Russ Maher was having with his application png files - http://xpagetips.blogspot.com/2014/11/got-png-you-may-get-problems.html. It got me thinking that as modern web developers (which is what XPage developers need to be), we should not be using Domino database elements for “web elements”. Back before R8 we all used the Database Files, Style sheets and Database Images as a way to reference files as part of the database. I now believe w [read] Keywords: domino R8 xpages application applications css database development server
248


XPages and Bluemix: How to create a Websockets message without being connected to the application
Sun, Nov 16th 2014 6:32p   Mark Roden
In this article I demonstrate how to create a Websocket message to be broadcast, from a website which is not connected to the Websocket directly. This will be achieved by using a http POST request of the message to a node.js server hosted on Bluemix and having that server then emit a Websocket message out to the application. Introduction In the previous article I demonstrated how to create an XPages chat client using a Bluemix hosted node.js server. I was able to do this by porting the node. [read] Keywords: ibm xpages ajax application javascript security server
299


One way to make a responsive XPages viewPanel
Tue, Nov 11th 2014 7:41p   Mark Roden
In this article I will demonstrate a method for hiding columns using CSS and making an XPages view Panel pseudo-responsive. What is Responsive Design? Responsive design is an approach to web page creation that makes use of flexible layouts, flexible images and cascading style sheet media queries. The goal of responsive design is to build web pages that detect the visitor’s screen size and orientation and change the layout accordingly. http://whatis.techtarget.com/definition/responsive-des [read] Keywords: xpages css mobile xml
1136


Creating an XPages Websockets chat client using Bluemix
Sun, Nov 9th 2014 7:53p   Mark Roden
In this article I will demonstrate how I was able to take an example Bluemix, node.js based, websocket chat program and re-purpose it to be used in XPages. Introduction Earlier this year I was very excited to find the Websockets in XPages project on OpenNTF published by Mark Ambler. The concept behind that project is to be able to create a notes document in a queue which is processed and then send out to all users. As much as I promised to help out and use the project, life and a business need [read] Keywords: domino ibm notes xpages ajax application applications css database dojo interface javascript mobile openntf server xml




146


Finding your localStorage values – Chrome Developer Tools
Thu, Nov 6th 2014 8:05p   Mark Roden
Just a quick tip – if you use localStorage and you need to find out what values you have in localStorage you can see them through developer tools easily. F12 to pull up developer tools Resources Local Storage is right there PS A similar feature will be release in the next Firefox (34) developer tools – https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector [read] Keywords: firefox
208


Because developers make mistakes – Webstorm Local History
Wed, Nov 5th 2014 10:24p   Mark Roden
Similar to Eclipse, Webstorm has a local history capability, allowing the developer who realizes they made an idiot mistake an hour ago, to go back to it and save their day. We’ve all been there and while committing to source control is a must for modern development, there are those times in between commits and then those when you are just too lazy to go through the repo looking for it. Local History Local History is always enabled in Webstorm – for the official help check here. U [read] Keywords: development eclipse
160


Conditional style loading in IE10+
Sun, Nov 2nd 2014 8:12p   Mark Roden
I learned this recently when trying to load a conditional stylesheet into an XPage. IE9 worked but IE11 failed on me and I couldn’t see why. It was only a small change so I ended up putting it in the main stylesheet as a media query. Apparently IE10 and above does not understand conditional HTML code like this http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx So to load a style conditionally in IE11 you can do it like this @media screen and (-ms-high-contrast: a [read] Keywords: css microsoft xml
258


Simple examples of how Google Developer Tools can aid Mobile Development
Thu, Oct 30th 2014 7:36p   Mark Roden
In this article I will show some simple examples of how Google Developer Tools can be used to help in mobile (responsive) development. Introduction Google Developer Tools (F12) within Chrome is one of those things which you *know* is way more powerful that you have ever cared to look at but this week I was introduced to a very cool new feature – thanks to @simonreid123. The ability to control the veiwport and size of the viewing window, as well as being able to throttle the speed of page [read] Keywords: development google iphone mobile network twitter




Created and Maintained by Yancy Lent - About - Blog Submission - Suggestions - Change Log - Blog Widget - Advertising - Mobile Edition