1

The link's code is as follows:

<td class="buttonColumn">
<a href="javascript:confirmDialog('Are you sure you want go?', 'http://google.com');" class="actionButtonNew">Leave to Google</a>
</td>

<td class="buttonColumn">
<a href="javascript:confirmDialog('Are you sure you want go?', 'http://yahoo.com');" class="actionButtonNew">Leave to Yahoo</a>
</td>

There are multiple buttons on the page with the exact same format that need to be changed as well. I was thinking something like this:

var fix = document.getElementsByClassName('actionButtonNew');
for (var i=0; i<fix.length; i++) {

I am not sure how to finish the code. I tried this:

var fix = document.getElementsByClassName('actionButtonNew');
for(var i = 0; i < fix.length; i++) {
    try {
        var l = fix[i];
        var h = fix[i].parentNode.getElementsByClassName('actionButtonNew')[0].href.replace("javascript:confirmDialog('Are you sure you want to go?',", "");
        l.href = h;
    } catch(e) {}
}

I know it is very messy and bad. It does change the link to: 'http://google.com');

So I think I may be on the right track. Please help me. Thanks in advance!

8
  • What exactly do you want to achieve with the links after they are changed (e.g. no link, remove all http://...)? Commented Mar 28, 2013 at 3:44
  • I want the link to direct to the website. ie: The Google link to go to google.com Commented Mar 28, 2013 at 3:45
  • So remove the confirmation? Commented Mar 28, 2013 at 3:45
  • Yes, remove the JavaScript conformation. Commented Mar 28, 2013 at 3:45
  • instead of replace, why don't you just do .href = "http://google.com" ? Commented Mar 28, 2013 at 3:48

3 Answers 3

1

Here's an idea: rather than deal with messy regular expressions that are bound to break on some edge case, replace confirmDialog with a function that redirects without confirming:

window.noConfirm = function(m, url) {
    document.location.href = url;
}

var fix = document.getElementsByClassName('actionButtonNew');
for (var i = 0; i < fix.length; i++) {
    fix[i].href = fix[i].href.replace('confirmDialog', 'noConfirm');
}

http://jsfiddle.net/AV4tB/

And another funny idea: change it call a function that immediately fixes the link, then trigger a click:

window.fixMe = function(m, url) {
    this.href = url;
}

var linksToFix = document.getElementsByClassName('actionButtonNew');
for (var i = 0; i < linksToFix.length; i++) {
    window.a = linksToFix[i];
    linksToFix[i].href = linksToFix[i].href.replace('confirmDialog(', 'fixMe.call(a, ');
    linksToFix[i].click();
}

http://jsfiddle.net/AV4tB/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, didn't think of that. Nice!
0

Try this:

var fix = document.getElementsByClassName('actionButtonNew');
for (var i = 0; i < fix.length; i++) {
    fix[i].href = fix[i].href.replace(/javascript:confirmDialog\(\'Are you sure you want go\?\'\, \'(http\:\/\/.*\.(.){2,3})'\);/gi,'$1');
}

Which uses regex to replace the confirm JS with just the links.
E.g. "javascript:confirmDialog('Are you sure you want go?', 'http://yahoo.com');" becomes "http://yahoo.com"

Comments

0

Pretty annoying to say to users "do you really want to do that?" every time they click on a link. After all, the back button will usually bring them straight back, or the escape key will cancel navigation if they're quick.

Since A elements may not all be links (some are targets), you can get all the links in a page using the document.links collection, then iterate over that:

// Function to call
function navPrompt() {
  var text = this.textContent || this.innerText
  return confirm('Are you sure you want to go to ' + text + '?');
}

// Attach to all listeners
window.onload = function() {

  var links = document.links;
  var i = links.length;
  while (i--) {
    links[i].onclick = navPrompt;
  }
}

On the otherhand, if you want to remove the comfirm box and change:

href="javascript:confirmDialog('Are you sure you want go?', 'http://google.com');"

to:

href="http://google.com"

you can do:

window.onload = function() {

  var link, links = document.links;
  var i = links.length;
  while (i--) {
    link = links[i];
    link.href = link.href.replace(/.*(http[^\'\"]+).+/i,'$1');
  }
}

Which will strip out the URL and assign it to the href property.

1 Comment

Could you make a jsFiddle link of it? I can't get it to work :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.