Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Need to add confirm (Yes or No) prompt to javascript function

Posted on 2008-10-17
4
1,765 Views
Last Modified: 2012-05-05
I need to update the following javascript function with a confirm dialog (ie: Yes or No prompt):

-----------------------------------------------------------------------------------------------------------------

// BetterInnerHTML v1.15 - by Craig Buckler, http://www.optimalworks.net/
function BetterInnerHTML(_1,_2,_3){function Load(_4){var _5;if(typeof DOMParser!="undefined"){_5=(new DOMParser()).parseFromString(_4,"application/xml");}else{var _6=["MSXML2.DOMDocument","MSXML.DOMDocument","Microsoft.XMLDOM"];for(var i=0;i<_6.length&&!_5;i++){try{_5=new ActiveXObject(_6[i]);_5.loadXML(_4);}catch(e){}}}return _5;}function Copy(_8,_9,_a){if(typeof _a=="undefined"){_a=1;}if(_a>1){if(_9.nodeType==1){var _b=document.createElement(_9.nodeName);for(var a=0,attr=_9.attributes.length;a<attr;a++){var _d=_9.attributes[a].name,aValue=_9.attributes[a].value,evt=(_d.substr(0,2)=="on");if(!evt){switch(_d){case "class":_b.className=aValue;break;case "for":_b.htmlFor=aValue;break;default:_b.setAttribute(_d,aValue);}}}_8=_8.appendChild(_b);if(evt){_8[_d]=function(){eval(aValue);};}}else{if(_9.nodeType==3){var _e=(_9.nodeValue?_9.nodeValue:"");var _f=_e.replace(/^\s*|\s*$/g,"");if(_f.length<7||(_f.indexOf("<!--")!=0&&_f.indexOf("-->")!=(_f.length-3))){_8.appendChild(document.createTextNode(_e));}}}}for(var i=0,j=_9.childNodes.length;i<j;i++){Copy(_8,_9.childNodes[i],_a+1);}}_2="<root>"+_2+"</root>";var _11=Load(_2);if(_1&&_11){if(_3!=false){while(_1.lastChild){_1.removeChild(_1.lastChild);}}Copy(_1,_11.documentElement);}}

-----------------------------------------------------------------------------------------------------------------

How would I do that?  Also, ... how would I update the link that triggers the function? :

<a href='javascript:BetterInnerHTML(document.getElementById("narrative"),"Text to be inserted");'>Insert</a>

Please advise.

Thanks,
- Yvan
0
Comment
Question by:egoselfaxis
  • 2
4 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22740425
I would start my first uncompacting javascript so i could see it clearly...all the line spaces have been removed...but the code is still the same.
0
 

Author Comment

by:egoselfaxis
ID: 22740482
Sorry - the library is not available in an uncompressed format.

http://www.optimalworks.net/resources/betterinnerhtml/

0
 
LVL 15

Accepted Solution

by:
MMDeveloper earned 500 total points
ID: 22740940
instead of trying to uncompress that.. try this


<a href='javascript:BetterInnerHTML_confirm(document.getElementById("narrative"),"Text to be inserted");'>Insert</a>


and the relevant JS
function BetterInnerHTML_confirm(_1, _2, _3) {
      message = "Are you sure?";
      response = confirm(message);
      
      if (response) {
            BetterInnerHTML(_1, _2, _3);
      }
      else {
            alert("canceled");
      }
}
0
 

Author Comment

by:egoselfaxis
ID: 22741648
Ah .. there we go. That worked great.

Thanks so much!

- Yvan
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript let vs var 5 34
JS does not refresh 6 28
How to pass JavaScript generated HTML text form values to PHP? 5 38
Responsive Font Size 6 24
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 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