从另一个页面调用Javascript函数?
我有两个html页面,第一个是index.html,另一个是content.html。 html页面包含一个JavaScript函数,我想从index.html页面调用该函数 我已将content.html页面嵌入index.html页面。 所以请建议我如何调用这个函数。 这是两页的代码。 Index.html从另一个页面调用Javascript函数?,javascript,html,Javascript,Html,我有两个html页面,第一个是index.html,另一个是content.html。 html页面包含一个JavaScript函数,我想从index.html页面调用该函数 我已将content.html页面嵌入index.html页面。 所以请建议我如何调用这个函数。 这是两页的代码。 Index.html <div id="content" class="bg-danger"> <embed src="content.html" id="child" width
<div id="content" class="bg-danger">
<embed src="content.html" id="child" width="100%" height="100%" style="height: 100%">
</embed>
</div>
Content.html
<body>
<script type="text/javascript">
function content2(){
alert("content");
}
</script>
<div>
Content Page
<button id="contentbtn" onclick="content();">Click</button>
</div>
函数content2(){
警报(“内容”);
}
内容页
点击
创建一个外部文件很容易,但条件是代码必须驻留在javascript html页面上。为什么不创建一个.js文件并用这个文件引用它
<script src="myscripts.js"></script>
然后,您可以在其他页面中使用
如果希望在网站的多个页面上运行相同的JavaScript,则应创建一个外部JavaScript文件,而不是反复编写相同的脚本
使用.js扩展名保存脚本文件,然后使用标记中的src属性引用它
<script src="javasriptFIle.js"></script>
- 使用外部javascript文件
- 在两个HTML文件中导入javascript文件
也许你应该考虑把JS放在一个文件中,然后从不同的HTML文件中调用它。
然后在html中用
引用JS您可以在项目中使用JQuery吗?将其添加到主页面,然后在嵌入的子页面中使用Jquery的document.ready(…)
或在主页面$(“child”).ready(…)代码>
如果没有,在主页上如何
document.getElementById(“child”).addEventListener('load',function(){//do something now child is loaded})代码>我知道制作外部javascript很容易,但条件是javascript代码必须驻留在该pade上,而不是任何外部文件。您不能从一个HTML调用嵌入在另一个HTML中的JS。时期谁设置了这个“条件”?使用iframe-javascript是可用的(跨源规则适用)Jaromanda请详细解释…如果其中一个页面是iframe,也可以从iframe中删除。