Solved

A href with OnClick

Posted on 2013-11-22
8
445 Views
Last Modified: 2013-11-22
Experts,

I have a link on my website like following and when I click on the link and user confirms to logout and based on my function it should logoff from
the current website and open up the new website window (which is working fine when user says yes).

The problem is, when user cancels from the alert box as per the code below, it is still opening up the new website which I don't want.
Is there anyway I can stop going/directing the user to new website when user cancels?

<a href="http://www.abcd.com/" title="Access abcd website" target="_blank" onClick="myFunction()">Click Here to go to ABCD Website</a>


function myFunction()
	{
	  if (confirm("Are you sure you want to logoff and go to ABCD website?") == true)
			{
				location.href = '/logoffpage.cfm?goto=loginpage';
			}
			else
			{			
				return;
			}
	}

Open in new window


Thanks in advance
Appreciate your help as this is little time sensitive.
0
Comment
Question by:Tpaul_10
8 Comments
 
LVL 1

Expert Comment

by:TechArtisan
ID: 39670393
This is a pretty easy fix I believe by removing the == true, since confirm already implies the okay button is chosen.  Try this:

function myFunction()
      {
        if (confirm("Are you sure you want to logoff and go to ABCD website?") )
                  {
                        location.href = '/logoffpage.cfm?goto=loginpage';
                  }
                  else
                  {                  
                        return;
                  }
      }
0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 39670398
or change your href to href="#"
0
 

Author Comment

by:Tpaul_10
ID: 39670427
Thanks for the quick response, appreciate it.

1. TechArtisan : Didn't work and still opens up the new website window.
2. dimmergeek : Didn't work either and with the # it is going to the current website when I cancels it.

if the user says yes, I need to direct the user to go to the new website and logoff from the current website (this is working fine with the current code)

and if the user says no/cancels nothing should happen. (it doesn't logoff from the current website but still going to the new website based on the current code)

Thanks
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 58

Accepted Solution

by:
Gary earned 400 total points
ID: 39670437
<a href="http://www.abcd.com/" title="Access abcd website" target="_blank" onClick="return myFunction()">Click Here to go to ABCD Website</a>

Open in new window


function myFunction()
      {
        if (confirm("Are you sure you want to logoff and go to ABCD website?"))
                  {
                        location.href = '/logoffpage.cfm?goto=loginpage';
                  }
                  else
                  {                  
                        return false;
                  }
      }

Open in new window

0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 39670439
Use the # in the link and remove your else clause
0
 
LVL 1

Expert Comment

by:TechArtisan
ID: 39670449
Yeah I noticed it didn't work after I posted it.  Try this instead.

I removed the link here as well as the _blank target:
<a href="#" title="Access abcd website" onClick="myFunction()">Click Here to go to ABCD Website</a>


function myFunction()
      {
        if (confirm("Are you sure you want to logoff and go to ABCD website?"))
                  {
                        location.href = '/logoffpage.cfm?goto=loginpage';
                        window.open('http://www.abcd.com/', '_blank');
                  }
                  else
                  {                  
                        return;
                  }
      }
0
 
LVL 1

Expert Comment

by:TechArtisan
ID: 39670455
GaryC123's suggestion works as well as my last one.  Either one does what your looking for.
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
ID: 39670457
Your code is a little confusing. When the user clicks on the LogOff link, and says Yes in the alert, do you want them to got to logoffpage.cfm or abcd.com?

In order to stop the link being followed if the user clicks on No, then your onClick() needs to return false. Your onClick needs to be like this:

onClick="return myFunction()"

And your function needs to be this:

function myFunction() {
    if (confirm("Are you sure you want to logoff and go to ABCD website?")) {
        location.href = '/logoffpage.cfm?goto=loginpage';
    } else {
        return false;
    }
}

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now