Tell me how you can insert file.html in file.js. For example, in PHP there is a function include (); And how to do it in Javascript?

  • four
    <script type = "text / javascript" src = "file.js"> </ script> - zvlex
  • one
    Well, this is only if the file is JS-code. HTML in its pure form in such files is not supported ... - AseN
  • In PHP, something like: <script> var html = <? = Json_encode ($ html)?> </ Script> But it’s better not to do so in order to get data from the server better, use ajax - Zowie

5 answers 5

JQuery example:

$("#content").load("page.html") 

page.html page into the element with id = 'content' .
Naturally, this will load the HTML into the current HTML, and not into JS. As already noted, it’s impossible to upload HTML to JS - think for yourself.

    include in javascript :) I answer on the subject, not the description.

     function includeScript(src) { var s = document.createElement('script'); s.src = src; s.async = false; s.type = 'text/javascript'; s.onload = arguments[1] || null; s.onreadystatechange = function() { if (this.readyState == 'complete' && typeof(this.onload) == 'function') this.onload(); } }; document.getElementsByTagName('head')[0].appendChild(s); return s; } 

    Usepage:

     includeScript('http://code.jquery.com/jquery-latest.js', function() { alert('jQuery v.'+$.fn.jquery+' loaded!'); }); 
    • one
      I didn’t realize at once how calbek is called until I paid attention to arguments[1] . It makes sense to add the second parameter to the function explicitly. It will be clearer. - KiTE

    You cannot insert .html into .js .

      unless the js code to dynamically create html elements and roll them into the code

        And you can use <iframe> or <frameset> anyway, the “factory” libraries do this with AJAX disabled

        • PS I wanted to answer a few days ago, but it didn’t work out ... Now I decided to answer all the same ... - Rules