0
votes

I have an xPage on which I have added an button. On this button there is some serverside script executed when clicked.

What I would like is whenever a user clicks on a link in the menu this code is executed. Therefore I created the following code on the link element.

 <xp:link loaded="true" escape="true" id="lnkTab"
                value="#{javascript:rootNode.getFrame();}">
                <xp:this.text><![CDATA[#{javascript://return rootNode.getLabel();
return rootNode.getLabel();}]]></xp:this.text>
                <xp:eventHandler event="onclick" submit="true"
                    refreshMode="complete">
                    <xp:this.action><![CDATA[#{javascript:sessionScope.selectedPage = rootNode;}]]></xp:this.action>
                    <xp:this.script><![CDATA[
var currentPage = "#{javascript: return this.getParent().getValue();}";
var currentPageLocation = window.location.href;
var lastSlash = currentPageLocation.lastIndexOf("/");

var page = currentPageLocation.substring(lastSlash+1,currentPageLocation.length);
if(page == "UserProfile.xsp" && currentPage != "UserProfile.xsp"){
    if(isProfileChanged()){
        debugger;
        var retValue = confirm("U heeft wijzigingen niet opgeslagen.\nWilt u deze alsnog opslaan?");
        if(retValue){
            sendRedirectSignal("#{javascript: rootNode.getFrame();}","#{id:txtRedirectSignal}","#{id:btnProfileSave}");
            thisEvent.preventDefault();
            thisEvent.stopPropagation();        

            sendRedirectSignal("#{javascript: rootNode.getFrame();}","#{id:txtRedirectSignal}","#{id:btnProfileSave}");
            return false;

    }
}}]]></xp:this.script>

                </xp:eventHandler>
            </xp:link>

the code for the sendRedirectSignal is as follows

function sendRedirectSignal(strPage,redirectId,btnId){
    var hidRedirect = dojo.byId(redirectId)
    var btnProfile = dojo.byId(btnId);
    if(hidRedirect!=null && btnProfile != null){
        console.log(btnProfile);
        hidRedirect.value = strPage;
        console.log(btnProfile.click());
    }

}

As you can see the code retrieves a button with dojo and issues the click event. For some reason this won't work at all. No event handlers are being executed on the button. When I execute the btn in firebug the code is being executed perfect. Can someone help on this issue? Why is the code not executed and how could I get it to work.

2

2 Answers

4
votes

This is a repeat of my answer to: Can I set scoped variable with onclick event in (link) column within Data Table?

Repeat after me: Client-side always takes precedence over server-side :-)

The value attribute for the xp:link control causes the server side event not to be executed at all. When clicking the link it just performs a HTTP GET to the resource in question.

So if you remove the value attribute, your onclick serverside event should be triggered.

0
votes

You could simplify that. Why put code in a button to have code in a link click that button? Two courses of action:

  • Remove the button (if you don't need it otherwise) and put the code in the link
  • If you need the button too: Move the code you have in the button into a SSJS function and call that function from the link and the button. a xp:link can execute SSJS too