views:

932

answers:

4

i am facing this issue:

<p class="pushButton">
  <a href="javascript:return;"
      onclick="SetOkCodeButton('=SC_ORDER', 'BBPForm', '', '',''); 
      return false;" name="Order" class="pushButton" 
      title="Orders your shopping cart. This completes the operation "
      onmouseover="status=' ';return true;"  onmouseout="status=' ';">
      &nbsp;Order&nbsp;
  </a>
</p>

<p class="pushButton">
  <a href="javascript:return;" 
      onclick="SetOkCodeButton('=SC_REFRESH', 'BBPForm', '', '',''); 
      return false;" name="Refresh" class="pushButton" 
      title="Updates all details according to your changes. "
      onmouseover="status=' ';return true;"  onmouseout="status=' ';">
      &nbsp;Refresh&nbsp;
   </a>
</p>

I need to change the functionality of the ORDER button. It should trigger the REFRESH (generating the SC_REFRESH event) functionality right before the SC_ORDER event is generated.

It means I need to trigger two events with one onlick on the ORDER button - first REFRESH and then ORDER. With 2 second time break between the two events. The functionality of REFRESH will not change.

+2  A: 

A). What's the reason for the 2-sec delay? This is extremely suspicious...

B). Any given method should achieve exactly one thing well. Creating different functionality for REFRESH and ORDER within the method suggests to me that you really have two methods which should be broken into two (which perhaps both call a common third function), but then again I'm struggling to imagine why you would want to refresh the form before posting it.

C). If you really need this you can introduce a timer with setTimeout preferably into the newly divided method itself, but if you continue with the existing implementation then I guess in the onclick attribute itself, though now it gets messy(er). You'd write something like this:

onclick="SetOkCodeButton('=SC_REFRESH', 'BBPForm', '', '',''); setTimeout(function(){SetOkCodeButton('=SC_ORDER', 'BBPForm', '', '','');},2000);"

Note though that setTimeout is asynchronous - if you want a real wait() method you'll need to write one yourself (and these are processor expensive), but they're expensive and again I doubt you really need it.

D). The return false is also deeply suspicious...

E). Likewise with the mouseouts and overs, is this auto-generated code?

annakata
The return false; statement cancels the default action of the link (navigating to the anchor location). Because of this the href="javascript:return;" attributes could simply be replaced with href="#".
Simon Lieschke
If you're putting that much code into the onclick attribute you'd be much better breaking it out to a function into a script block for readability and ease of maintenance.
Simon Lieschke
onclick doesn't require return false, hrefs do (and javascript: void foo() is a safer, clearer way to do that - and I couldn't agree more that this is horrible code - see points A,B,D and E :)
annakata
If you have a href="#" attribute on the link as I described the return false statement at the end of the onclick handler is necessary if you want to prevent the browser from scrolling to the top of your page.
Simon Lieschke
Using the JavaScript pseudo protocol can result in obscure problems in Internet Explorer: http://blog.reindel.com/2006/08/11/a-hrefjavascriptvoid0-avoid-the-void/http://jibbering.com/faq/#javascriptURI details further why it's best to avoid using the JavaScript pseudo-protocol in anchor hrefs.
Simon Lieschke
Well yeah there's a lot of strikes against this in general (which is why I'd avoid it entirely and work with JS event binding) but given the lack of choice I find the risk/burden of return false > than the risk of an obscure IE bug with a bad img swap technique anyway. Basically it all sucks.
annakata
A: 

Thanx for your reactions guys. the timeout is not so much important. it is just my guess to try it out. The main goal is to REFRESH my screen before the ORDER event goes on when user clicks ORDER. Now I have two pushbuttons: REFRESH and ORDER . The code is working in our SRM web -> SAP enviroment. It is a template.

I am not so good in Javascript so I though someone can give me some hint.

*why* do you want to refresh the screen though? you can't reload the page without dropping the event and you can't refresh->submit without losing data *unless* you're doing somethign to preserve the data. It makes more sense to reload *afterwards*. Tell us what your goal is.
annakata
+1  A: 

Any reason this won't work?

<p class="pushButton">
  <a href="javascript:return;" 
      onclick="SetOkCodeButton('=SC_REFRESH', 'BBPForm', '', '','');SetOkCodeButton('=SC_ORDER', 'BBPForm', '', '','');
      return false;" name="Refresh" class="pushButton" 
      title="Updates all details according to your changes. "
      onmouseover="status=' ';return true;"  onmouseout="status=' ';">
       Refresh 
   </a>
</p>

If those functions are synchronous, this should be OK, if they're async, you'll likely need a callback.

Mainguy
A: 

I just found out that the code I listed here is generated by this template from SAP. I am new in this. :-) Anybody can see a chance to trigger 2 events (SC_REFRESH and SC_ORDER) instead of just one (SC_ORDER)?

TR() TD()

BBPVSpace() BBPButtonBegin()

if (BTN_SC_ORDER.exists) if (BTN_SC_ORDER.disabled) BBPDisabledButton(BTN_SC_ORDER.label) BBPButtonSpace() else BBPButton(BTN_SC_ORDER.OKCODE,BTN_SC_ORDER.label, tooltip=#BUTTON_ORDER) BBPButtonSpace() end end

if (BTN_SC_REFRESH.exists) if (BTN_SC_REFRESH.disabled) BBPDisabledButton(BTN_SC_REFRESH.label) BBPButtonSpace() else BBPButton(BTN_SC_REFRESH.OKCODE,BTN_SC_REFRESH.label,tooltip=#BUTTON_ACTUAL) BBPButtonSpace() end end

related questions