简体   繁体   English

如何动态包含包含javascript的html文件

[英]How to dynamically include a html file containing javascript

in my website i have a simple navigation bar where clicking on the different items causes the main content to change. 在我的网站上,我有一个简单的导航栏,在其中单击不同的项目会导致主要内容发生变化。 Technically, all is done in a single file "main.html", which contains everything and i use the jquery function "load" to dynamically load other html files to place it inside the main.html. 从技术上讲,所有操作都在一个包含所有内容的文件“ main.html”中完成,我使用jquery函数“ load”动态加载其他html文件以将其放置在main.html中。 This works just fine as long as there is no javascript in these html files i embedded. 只要嵌入的这些html文件中没有javascript,它就可以正常工作。 But there is one file i want to embedd, which has also javascript code in it, which should get executed on $(document).ready. 但是我要嵌入一个文件,其中也包含javascript代码,该文件应在$(document).ready上执行。 But this never happens - there is no error in the javascript, it just never gets executed. 但这永远不会发生-javascript中没有错误,只是永远不会执行。 I suppose it's because i only change the DOM of the "main.html" and then there will be no "onReady" event fired. 我想这是因为我只更改了“ main.html”的DOM,然后就不会触发“ onReady”事件。 Can someone give me some idea of what would be the best way to get the desired behaviour? 有人可以给我一些想法,以获得理想的行为的最佳方法吗? Placing the javascript in the "main.html" would cause things to work, but this is no option for me. 将javascript放置在“ main.html”中会导致一切正常,但这对我来说是没有选择的。

Thanks in advance! 提前致谢!

update: problem solved Problem is solved. 更新:问题已解决问题已解决。 When i call the javascript outside the onReady() event (as Loic Coenen suggested), it works just as excpected. 当我在onReady()事件之外调用javascript时(正如Loic Coenen建议的那样),它的工作原理与预期的一样。 I also removed the tags wrapped around. 我还删除了包裹的标签。 Many thanks to all who helped me! 非常感谢所有帮助我的人!

you are in main.html that contains a head, body. 您位于main.html ,其中包含头部,身体。 the problem might be that when you load the page your other page also has a head, and body and so on, if you are loading onto a div, it should be only code, can be javascript aswell, but no head or body. 问题可能是,当您加载页面时,其他页面上也有head和body等,如果您加载到div上,则应该仅仅是代码,也可以是javascript,而没有head或body。 but if you resist, here is your solution 但是如果您抵抗,这就是您的解决方案

$("#loadnewpagebttn").load(location.href+" foobar ",function(){
    $.getScript("js/yourscript.js"); 
});

you need to create a script file for that page and load it. 您需要为该页面创建脚本文件并加载它。 inline scripts are some what of a bad habit anyways 内联脚本还是个坏习惯

full example: 完整的例子:

jQuery.getScript( url, [ success(data, textStatus) ] )
url - A string containing the URL to which the request is sent.

success(data, textStatus) - A callback function that is executed if the request succeeds.

$.getScript('ajax/test.js', function() {
  alert('Load was performed.');
});

http://api.jquery.com/jQuery.getScript/ http://api.jquery.com/jQuery.getScript/

You can trigger a custom event to notify your other.html script that the tree is loaded. 您可以触发一个自定义事件,以通知other.html脚本树已加载。 I haven't tried it, but I would use something like that : 我没有尝试过,但是我会使用类似的东西:

In main.html: 在main.html中:

$('#div_to_load').load('other.html',{},function(){
    $('#div_to_load').trigger('loaded')
})

In other.html 在other.html

$('#div_to_load').on('loaded', function(){
     // Code to execute  
})

I don't know if that could do the trick. 我不知道这是否可以解决问题。

Edit : I asume that the javascript directly included in your other.html is executed anyway. 编辑:我想无论如何都直接执行了您other.html包含的javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM