2016-06-29 13 views
0

javascriptとbootstrapモーダルダイアログを使って、便利な汎用メッセージボックスを作成しました。私(そして潜在的に他人)はどこでもそれを使うことができます。私はjsファイルに展開したいので、他のプロジェクトのjsファイルを参照するだけです。しかし、私はブートストラップモーダルダイアログのHTMLコードブロックをどのように含めるか分かりません。jsファイルにHTMLを含めるにはどうすればいいですか?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
    <title>A useful generic message box</title> 
 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
 

 
    <link rel="stylesheet" type="text/css" href="~/Content/bootstrap.min.css" media="screen" /> 
 
    <script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script> 
 
    <script src="~/Scripts/bootstrap.js" type="text/javascript"></script> 
 

 

 
    <script type="text/javascript"> 
 
     function testAlert() { 
 
      messageBox('Something went wrong!', 'error', null, function() { 
 
       alert('Message dialog closed.'); 
 
      }); 
 
     } 
 

 
     function testConfirm() { 
 
      messageBox('Do you want to proceed?', 'warning', { okButtonName: 'Yes', cancelButtonName: 'No' }, function() { 
 
       alert('"Yes" was selected.'); 
 
      }); 
 
     } 
 

 
    function testPrompt() { 
 
     messageBox('How do you feel now?', 'normal', { showTextBox: true }, function (event) { 
 
      alert('User entered "' + event.data.getUserInput() + '".'); 
 
     }); 
 
    } 
 

 
     function messageBox(msg, significance, options, actionConfirmedCallback) { 
 
      var okButtonName, cancelButtonName, showTextBox; 
 

 
      if (options == null) { 
 
       okButtonName = 'OK'; 
 
       cancelButtonName = null; 
 
       showTextBox = null; 
 
      } else { 
 
       okButtonName = options.okButtonName; 
 
       cancelButtonName = options.cancelButtonName; 
 
       showTextBox = options.showTextBox; 
 
      } 
 
       
 

 
      if (showTextBox == true) 
 
       $('#MessageDialogTextArea').show(); 
 
      else 
 
       $('#MessageDialogTextArea').hide(); 
 

 
      //if (typeof (okButtonName) != 'undefined') 
 
      if (okButtonName != null) 
 
       $('#messageDialogOkButton').html(okButtonName); 
 
      else 
 
       $('#messageDialogOkButton').html('OK'); 
 

 
      //if (typeof (cancelButtonName) == 'undefined') 
 
      if (cancelButtonName == null) 
 
       $('#messageDialogCancelButton').hide(); 
 
      else { 
 
       $('#messageDialogCancelButton').show(); 
 
       $('#messageDialogCancelButton').html(cancelButtonName); 
 
      } 
 

 
      $('#messageDialogOkButton').unbind('click'); 
 

 
      if (typeof (actionConfirmedCallback) != 'undefined') 
 
       $('#messageDialogOkButton').on('click', { getUserInput: getUserInput }, actionConfirmedCallback); 
 
      else 
 
       $('#messageDialogOkButton').removeAttr('onclick'); 
 

 
      var content = $("#MessageDialogContent"); 
 

 
      if (significance == 'error') 
 
       content.attr('class', 'text-danger'); 
 
      else if (significance == 'warning') 
 
       content.attr('class', 'text-warning'); 
 
      else 
 
       content.attr('class', 'text-success'); 
 

 
      content.html(msg); 
 
      $("#MessageDialog").modal(); 
 
     } 
 
    </script> 
 
</head> 
 

 
<body> 
 

 

 
    <a href="#" onclick="testAlert();">Test alert</a> <br/> 
 
    <a href="#" onclick="testConfirm();">Test confirm</a> <br/> 
 
    <a href="#" onclick="testPrompt();">Test prompt</a> 
 

 
    <div class="modal fade" id="MessageDialog" role="dialog"> 
 
     <div class="modal-dialog"> 
 
      <div class="modal-content"> 
 
       <div class="modal-body"> 
 
        <p class="text-success" id="MessageDialogContent">Some text in the modal.</p> 
 
        <p><textarea id="MessageDialogTextArea" cols="70" rows="5"></textarea></p> 
 
       </div> 
 
       <div class="modal-footer"> 
 
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="messageDialogOkButton">OK</button> 
 
        <button type="button" class="btn btn-default" data-dismiss="modal" id="messageDialogCancelButton">Cancel</button> 
 
       </div> 
 
      </div> 
 
     </div> 
 
    </div> 
 

 
</body> 
 

 
</html>

+0

を[JSにHTMLコードを含む](http://stackoverflow.com/questions/12216391/includeの可能性のある複製を-html-code-in-js) –

+0

テンプレートエンジンを使用する必要があります。 – SLaks

答えて

1

それはおろか、合理的な、これを行うことは非常に慣用的ではありません。 ES5準拠が必要ないと仮定すると、テンプレートリテラルにすべてをダンプすることができます。それからあなたはそれをスクリプトのどこかのドームに押し込みます。

はこのHTMLを取り、それ自身のファイルmodal.htmlにそれを置く:ここ

const template = ` 
    <div class="modal fade" id="MessageDialog" role="dialog"> 
     <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-body"> 
        <p class="text-success" id="MessageDialogContent">Some text in the modal.</p> 
        <p><textarea id="MessageDialogTextArea" cols="70" rows="5"></textarea></p> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="messageDialogOkButton">OK</button> 
        <button type="button" class="btn btn-default" data-dismiss="modal" id="messageDialogCancelButton">Cancel</button> 
       </div> 
      </div> 
     </div> 
    </div> 
` 

$('somehidden div').html(template); 

はES5バージョン

var template = '<div class="modal fade" id="MessageDialog" role="dialog">'+ 
    ' <div class="modal-dialog">'+ 
    '  <div class="modal-content">'+ 
    '   <div class="modal-body">'+ 
    '    <p class="text-success" id="MessageDialogContent">Some text in the modal.</p>'+ 
    '    <p><textarea id="MessageDialogTextArea" cols="70" rows="5"></textarea></p>'+ 
    '   </div>'+ 
    '   <div class="modal-footer">'+ 
    '    <button type="button" class="btn btn-primary" data-dismiss="modal" id="messageDialogOkButton">OK</button>'+ 
    '    <button type="button" class="btn btn-default" data-dismiss="modal" id="messageDialogCancelButton">Cancel</button>'+ 
    '   </div>'+ 
    '  </div>'+ 
    ' </div>'+ 
    '</div>'; 
$('somehidden div').html(template); 

UPDATE

より保守ソリューションです。

<div class="modal fade" id="MessageDialog" role="dialog"> 
     <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-body"> 
        <p class="text-success" id="MessageDialogContent">Some text in the modal.</p> 
        <p><textarea id="MessageDialogTextArea" cols="70" rows="5"></textarea></p> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="messageDialogOkButton">OK</button> 
        <button type="button" class="btn btn-default" data-dismiss="modal" id="messageDialogCancelButton">Cancel</button> 
       </div> 
      </div> 
     </div> 
    </div> 

私はetherealiteの助けを借りてそれを行う方法を働いたmodal.js

 messageBox('Something went wrong!', 'error', null, function() { 
      alert('Message dialog closed.'); 
     }); 
    } 

    function testConfirm() { 
     messageBox('Do you want to proceed?', 'warning', { okButtonName: 'Yes', cancelButtonName: 'No' }, function() { 
      alert('"Yes" was selected.'); 
     }); 
    } 

function testPrompt() { 
    messageBox('How do you feel now?', 'normal', { showTextBox: true }, function (event) { 
     alert('User entered "' + event.data.getUserInput() + '".'); 
    }); 
} 

    function messageBox(msg, significance, options, actionConfirmedCallback) { 
     var okButtonName, cancelButtonName, showTextBox; 

     if (options == null) { 
      okButtonName = 'OK'; 
      cancelButtonName = null; 
      showTextBox = null; 
     } else { 
      okButtonName = options.okButtonName; 
      cancelButtonName = options.cancelButtonName; 
      showTextBox = options.showTextBox; 
     } 


     if (showTextBox == true) 
      $('#MessageDialogTextArea').show(); 
     else 
      $('#MessageDialogTextArea').hide(); 

     //if (typeof (okButtonName) != 'undefined') 
     if (okButtonName != null) 
      $('#messageDialogOkButton').html(okButtonName); 
     else 
      $('#messageDialogOkButton').html('OK'); 

     //if (typeof (cancelButtonName) == 'undefined') 
     if (cancelButtonName == null) 
      $('#messageDialogCancelButton').hide(); 
     else { 
      $('#messageDialogCancelButton').show(); 
      $('#messageDialogCancelButton').html(cancelButtonName); 
     } 

     $('#messageDialogOkButton').unbind('click'); 

     if (typeof (actionConfirmedCallback) != 'undefined') 
      $('#messageDialogOkButton').on('click', { getUserInput: getUserInput }, actionConfirmedCallback); 
     else 
      $('#messageDialogOkButton').removeAttr('onclick'); 

     var content = $("#MessageDialogContent"); 

     if (significance == 'error') 
      content.attr('class', 'text-danger'); 
     else if (significance == 'warning') 
      content.attr('class', 'text-warning'); 
     else 
      content.attr('class', 'text-success'); 

     content.html(msg); 
     $("#MessageDialog").modal(); 

が文書

<html> 
<head> 
    <script type="text/javascript> 
    $(function() { 
     $(body).load("path/to/modal.html"); 
    }); 
    </script> 
<script src="path/to/modal.js" type="text/javascript"></script> 
</head> 
+0

私が必要とするのは、ブートストラップモーダルダイアログとmesssageBox関数(外部ライブラリ)を作成する方法です。コードブロックをすべてのプロジェクトに複製する必要はありません。目標は少なくともエレガントではないですか?この優雅な目標を達成するためにエレガントだと思うソリューションがあれば、私はそれを受け入れます。私はjavascriptの専門家ではないので、私の質問に答えるために私のコードを使用してください。ありがとう! –

+0

答えを編集することはできますか?つまり、元の回答を削除して新しい回答を投稿するのではなく、元の回答に変更を加えることができました。 – nnnnnn

+0

回答をもっと整備可能な解決策で更新しました。それはテストされていませんが、それは正しい道にあなたを設定する必要があります。 – etherealite

0

enter image description hereでそれを使用して、あなたのJSを取り、それ自身のファイルに入れ彼の答えは完全ではなく、正しい方向を指しています。

それはbootbox.jsを行うことができますすべてを行うことができ

  • ので、それは、bootbox.js
    よりも優れています。
  • 利用の構文は、それはあなたがエレガントな「エラー」、「警告」または「情報」
  • Bootboxが
  • を鉱山はわずか110行の長、986行の長さであるを使用して、メッセージの色を制御することができます
  • 簡単です

digimango.messagebox.js

const dialogTemplate = '\ 
 
    <div class ="modal" id="digimango_messageBox" role="dialog">\ 
 
     <div class ="modal-dialog">\ 
 
      <div class ="modal-content">\ 
 
       <div class ="modal-body">\ 
 
        <p class ="text-success" id="digimango_messageBoxMessage">Some text in the modal.</p>\ 
 
        <p><textarea id="digimango_messageBoxTextArea" cols="70" rows="5"></textarea></p>\ 
 
       </div>\ 
 
       <div class ="modal-footer">\ 
 
        <button type="button" class ="btn btn-primary" id="digimango_messageBoxOkButton">OK</button>\ 
 
        <button type="button" class ="btn btn-default" data-dismiss="modal" id="digimango_messageBoxCancelButton">Cancel</button>\ 
 
       </div>\ 
 
      </div>\ 
 
     </div>\ 
 
    </div>'; 
 

 

 
// See the comment inside function digimango_onOkClick(event) { 
 
var digimango_numOfDialogsOpened = 0; 
 

 

 
function messageBox(msg, significance, options, actionConfirmedCallback) { 
 
    if ($('#digimango_MessageBoxContainer').length == 0) { 
 
     var iDiv = document.createElement('div'); 
 
     iDiv.id = 'digimango_MessageBoxContainer'; 
 
     document.getElementsByTagName('body')[0].appendChild(iDiv); 
 
     $("#digimango_MessageBoxContainer").html(dialogTemplate); 
 
    } 
 

 
    var okButtonName, cancelButtonName, showTextBox, textBoxDefaultText; 
 

 
    if (options == null) { 
 
     okButtonName = 'OK'; 
 
     cancelButtonName = null; 
 
     showTextBox = null; 
 
     textBoxDefaultText = null; 
 
    } else { 
 
     okButtonName = options.okButtonName; 
 
     cancelButtonName = options.cancelButtonName; 
 
     showTextBox = options.showTextBox; 
 
     textBoxDefaultText = options.textBoxDefaultText; 
 
    } 
 

 
    if (showTextBox == true) { 
 
     if (textBoxDefaultText == null) 
 
      $('#digimango_messageBoxTextArea').val(''); 
 
     else 
 
      $('#digimango_messageBoxTextArea').val(textBoxDefaultText); 
 

 
     $('#digimango_messageBoxTextArea').show(); 
 
    } 
 
    else 
 
     $('#digimango_messageBoxTextArea').hide(); 
 

 
    if (okButtonName != null) 
 
     $('#digimango_messageBoxOkButton').html(okButtonName); 
 
    else 
 
     $('#digimango_messageBoxOkButton').html('OK'); 
 

 
    if (cancelButtonName == null) 
 
     $('#digimango_messageBoxCancelButton').hide(); 
 
    else { 
 
     $('#digimango_messageBoxCancelButton').show(); 
 
     $('#digimango_messageBoxCancelButton').html(cancelButtonName); 
 
    } 
 

 
    $('#digimango_messageBoxOkButton').unbind('click'); 
 
    $('#digimango_messageBoxOkButton').on('click', { callback: actionConfirmedCallback }, digimango_onOkClick); 
 

 
    $('#digimango_messageBoxCancelButton').unbind('click'); 
 
    $('#digimango_messageBoxCancelButton').on('click', digimango_onCancelClick); 
 

 
    var content = $("#digimango_messageBoxMessage"); 
 

 
    if (significance == 'error') 
 
     content.attr('class', 'text-danger'); 
 
    else if (significance == 'warning') 
 
     content.attr('class', 'text-warning'); 
 
    else 
 
     content.attr('class', 'text-success'); 
 

 
    content.html(msg); 
 

 
    if (digimango_numOfDialogsOpened == 0) 
 
     $("#digimango_messageBox").modal(); 
 

 
    digimango_numOfDialogsOpened++; 
 
} 
 

 
function digimango_onOkClick(event) { 
 
    // JavaScript's nature is unblocking. So the function call in the following line will not block, 
 
    // thus the last line of this function, which is to hide the dialog, is executed before user 
 
    // clicks the "OK" button on the second dialog shown in the callback. Therefore we need to count 
 
    // how many dialogs is currently showing. If we know there is still a dialog being shown, we do 
 
    // not execute the last line in this function. 
 
    if (typeof (event.data.callback) != 'undefined') 
 
     event.data.callback($('#digimango_messageBoxTextArea').val()); 
 

 
    digimango_numOfDialogsOpened--; 
 

 
    if (digimango_numOfDialogsOpened == 0) 
 
     $('#digimango_messageBox').modal('hide'); 
 
} 
 

 
function digimango_onCancelClick() { 
 
    digimango_numOfDialogsOpened--; 
 

 
    if (digimango_numOfDialogsOpened == 0) 
 
     $('#digimango_messageBox').modal('hide'); 
 
}

digimango.messagebox.js使用するには:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
    <title>A useful generic message box</title> 
 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
 

 
    <link rel="stylesheet" type="text/css" href="~/Content/bootstrap.min.css" media="screen" /> 
 
    <script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script> 
 
    <script src="~/Scripts/bootstrap.js" type="text/javascript"></script> 
 
    <script src="~/Scripts/bootbox.js" type="text/javascript"></script> 
 

 
    <script src="~/Scripts/digimango.messagebox.js" type="text/javascript"></script> 
 

 

 
    <script type="text/javascript"> 
 
     function testAlert() { 
 
      messageBox('Something went wrong!', 'error'); 
 
     } 
 

 
     function testAlertWithCallback() { 
 
      messageBox('Something went wrong!', 'error', null, function() { 
 
       messageBox('OK clicked.'); 
 
      }); 
 
     } 
 

 
     function testConfirm() { 
 
      messageBox('Do you want to proceed?', 'warning', { okButtonName: 'Yes', cancelButtonName: 'No' }, function() { 
 
       messageBox('Are you sure you want to proceed?', 'warning', { okButtonName: 'Yes', cancelButtonName: 'No' }); 
 
      }); 
 
     } 
 

 
     function testPrompt() { 
 
      messageBox('How do you feel now?', 'normal', { showTextBox: true }, function (userInput) { 
 
       messageBox('User entered "' + userInput + '".'); 
 
      }); 
 
     } 
 

 
     function testPromptWithDefault() { 
 
      messageBox('How do you feel now?', 'normal', { showTextBox: true, textBoxDefaultText: 'I am good!' }, function (userInput) { 
 
       messageBox('User entered "' + userInput + '".'); 
 
      }); 
 
     } 
 

 
    </script> 
 
</head> 
 

 
<body> 
 
    <a href="#" onclick="testAlert();">Test alert</a> <br/> 
 
    <a href="#" onclick="testAlertWithCallback();">Test alert with callback</a> <br /> 
 
    <a href="#" onclick="testConfirm();">Test confirm</a> <br/> 
 
    <a href="#" onclick="testPrompt();">Test prompt</a><br /> 
 
    <a href="#" onclick="testPromptWithDefault();">Test prompt with default text</a> <br /> 
 
</body> 
 

 
</html>

関連する問題