2 Replies - 755 Views - Last Post: 23 February 2014 - 01:34 PM

#1 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 3
  • View blog
  • Posts: 557
  • Joined: 06-July 13

JavaScript Confirm

Posted 01 February 2014 - 04:07 AM

I've been looking into some of the tutorials on w3schools, like this one: http://www.w3schools...yjsref_confirm2 and I've been trying to create a confirm box that will either go to a specified URL or reload the current page. The reload works fine, but whenever I click "OK" it goes to my website's homepage: http://www.strongholdnation.co.uk/ - When I do it on my desktop, it opens my desktop folder. Why?

<!DOCTYPE html>
<html>
<body>

<a href="" id="remove" onclick="javascript:remove_comment()">Try it</a>

<script>
function remove_comment()
{
	var x;
	var r=confirm("Are you sure you wish to remove this comment?");
	if (r==true)
	{
		window.location.href = 'http://google.co.uk';  
	}
	else
	{
		location.reload()
	}
	document.getElementById("remove").innerHTML=x;
}
</script>

</body>
</html>


This post has been edited by chris98: 01 February 2014 - 04:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript Confirm

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: JavaScript Confirm

Posted 01 February 2014 - 07:08 AM

The problem is that your <a> element has an empty "href" attribute set. This will cause the browser to redirect to the current page. Your location changes inside the function will be overridden by that.

So you have two choices: pass the event object to the remove_comment function and cancel the default behaviour, or remove the "href" attribute and simulate the link appearance through CSS. (I'd recommend the latter; it's a simpler solution.)
Was This Post Helpful? 0
  • +
  • -

#3 felgall  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 76
  • Joined: 22-February 14

Re: JavaScript Confirm

Posted 23 February 2014 - 01:34 PM

Hasn't w3schools updated that page yet to bring it into the 21st century?

Since Netscape 4 died it has been possible to create your own in page dialog boxes that look and function the way that you want. The confirm dialog was then repurposed as a debugging tool with additional checkboxes added in some browsers to offer the person a third option of either disabling Javascript completely for the page or disabling all future dialog boxes for the page (depending on which browser it is). With all browsers now having a built in debugger even this purpose for the confirm dialog is no longer needed and some people simply disable it completely in their browser.

If you want everyone to actually see your message you should generate the "dialog box" yourself inside the web page.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1