Link to home
Start Free TrialLog in
Avatar of kpbarem

asked on

How to account for users who don't have javascript enabled when linking to a different page with javascript

I have an <a> tag that links to an external website.  I have a fairly simple function that uses a confirm box to let the user know that they are about to leave the current website and if they click proceed then they are redirected to the external website.  If they click cancel then nothing happens.  If users don't have javascript enabled I'd like it to just go ahead and send them to the link without the confirmation.  I'm not quite sure how to account for that.  Can anyone help me out?
Avatar of Gary
Flag of Ireland image

Just have a normal link in the href.
Don't do anything - the javascript won't fire if not enabled and the <a> tag will fail over to do what it normally does.
Avatar of dimmergeek
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kpbarem


I do have a normal link in the href.  I actually expected that to be the behavior but it goes to the link anyway even after clicking cancel, at least when running locally.  Is there something wrong with my script?

            function confirmMethod() {
                if (confirm("You are about to exit  Would you like to proceed?")) {
                else {

 <a href="" target="_top" onclick="confirmMethod()">Book Hotel Room</a>
That wasnt your question

 <a href="" target="_top" onclick="return confirmMethod()">Book Hotel Room</a>
<script type="text/javascript">
 function confirmMethod() {
      var answer = confirm("You are about to exit  Would you like to proceed?")
                if (!answer){return false;}
Avatar of kpbarem


This worked great.   Thanks for everyones help.
How does the accepted answer answer your question?
with you on this one Gary - just re-read the original post to see if I did my usual missed the point trick but seems pretty clear on what was asked which does not match final solution.
My interpretation of the question is that he is looking for a way to use a confirm box for users with javascript to ensuyre they want to leave his parent site, and just a plain link for users with no javascript enabled.

The answer I provided does that.
If I misinterpreted his question, then I am wrong.  What were other interpretations?

Perhaps the original poster can clarify whether I misinterpreted or not??
@dimmergeek - yes it does but it is not necessary in this case to use the <noscript> tags

<a href="" onclick="return warning();">Link</a>

Will satisfy the requirements as well. If JS not enabled the link will fire - if not the warning will popup.

Both Gary and myself allueded to this in our original posts.
But then he clarified his question saying when he clicks Cancel it still follows the link.