[thelist] Re: javascript debug (easy)

Phil Turmel philip at turmel.org
Mon Jun 6 12:30:26 CDT 2005


Tyme,

Don't dismiss Matt's solution too quickly...  John missed an important 
part of it: If the original function returns (true), after already 
running a window.open() method, then both the new window and the 
original window will go to the new location.  Changing 'return false' to 
'return agree' is wrong.

Matt's code has the advantage of permitting graceful fallback when 
JavaScript is disabled.  In general, if an 'onclick' handler uses 
window.open, it must return false to avoid doubling up.  In the case of 
a false from confirm(), the handle must also return false to avoid 
opening the page.

If the application requires JavaScript for proper operation, put a url 
for some kind of "JavaScript Required" error page in the link itself, 
and the correct page in the onclick handler.

HTH,

Phil

Tyme wrote:
> Well, I am slightly less embarrased by my original ("easy") question, 
> since it sparked an actual discussion.
> 
> Matt's suggestion did work.  But, I am all about writing more concise 
> code. (I suspect that Matt was just correcting my bloated code to work, 
> not writing what he himself would have created from scratch.)   In all 
> fairness to both Matt and John, I'll write a script that will 
> alternately load one version one day, the other the next.  :-)
> 
> Okay, I might need a wee bit of help on that alternating load script.  
> Heh heh.
> 
>> From the ridiculous to the sublime,
> 
> Tyme
> ----- Original Message ----- From: "Brooking, John" 
> <John.Brooking at sappi.com>
> To: <thelist at lists.evolt.org>
> Sent: Monday, June 06, 2005 10:29 AM
> Subject: [thelist] Re: javascript debug (easy)
> 
> 
> Tyme tried:
> 
>> function openOrderForm(){
>>
>> var agree=confirm("Online ordering is reserved for weekly rentals only.
> 
> 
>> Please phone for daily orders. Thank you!");
>>
>> IF (agree == true)
>>
>> window.open('https://boomersrentals.c2.ixwebhosting.com/orderform.asp',
> 
> 'OrderForm','scrollbars=yes,toolbar=no,directories=no,menubar=yes,resiza
> ble=yes,status=yes,width=300,height=400,screenX=0,screenY=0,top=0,left=0
> ');
> 
>>
>> return true;
>>
>> ELSE
>>
>> return false;
>> }
> 
> 
> Matt suggested:
> 
>> function openOrderForm()
>> {
>>
>>    var agree=confirm(...);
>>
>>    if (agree)
>>    {
>>        window.open(...);
>>    } // end if
>>
>>    return false;
>> }
> 
> 
> But Matt, your version doesn't return anything if agree is true. I'd
> change the "return false" to "return agree".
> 
> Since I'm a sucker for concise code, I'd be tempted to use this
> maximally shortened expression:
> 
> function openOrderForm() {
>   return confirm( ... )
>          ? window.open(...)
>          : false ;
> }
> 
> Now why stop there? Create a generic function which takes a prompt and a
> URL, and does the same logic for any window you want to open from
> anywhere:
> 
> function openNewWindow( prompt, url )
>   return confirm(prompt) ? window.open(url) : false;
> }
> 
> You may have noticed that these functions return the handle of the
> opened window, or false if no window was opened. I've found it possible
> to evaluate this return value in an "if" statement and have the window
> handle act as a "true" value, but I'm not sure how supported this is in
> the official specs.
> 
> - John




More information about the thelist mailing list