By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Conditional option in push-button?

P: n/a
Hi all.
Can a conditional be applied to a button?
I want user to go back if they have come from a previous page
or to simply close window if they have not. Tried this but no go!

<INPUT type="button" value="CLOSE WINDOW" IF history.
onClick="history.back(-1) else onClick="javascript:window.close();">
Sep 7 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Richard Rosser said the following on 9/7/2006 7:17 AM:
Hi all.
Can a conditional be applied to a button?
I want user to go back if they have come from a previous page
or to simply close window if they have not. Tried this but no go!

<INPUT type="button" value="CLOSE WINDOW" IF history.
onClick="history.back(-1) else onClick="javascript:window.close();">
The best you could do is to check document.referrer to see if they came
from a linked page or not. You can't check the history trail as it's a
security breach.

onclick="if(document.referrer){history.back(-1)}else{window.close()}"

But it is still fraught with errors. If I right click a link and open in
a new window/tab then the referrer will be empty and the script would
attempt to close the window. Yet, since the window/tab wasn't opened
with script you will get the "Script is trying to close a window...."
message in IE and an error message in the Console in Firefox.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 7 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.