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

#1 chris98  Icon User is offline

  • D.I.C Lover

Reputation: 40
  • View blog
  • Posts: 1,104
  • 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>

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

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';  


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

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • 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 Regular

Reputation: 68
  • View blog
  • Posts: 365
  • 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